Add a force to an object to make it move toward another with a permanent force
How I do it?
Add a force to an object to make it move toward another with a permanent force
How I do it?
scene.physics.moveToObject(myobject, targetobject, myspeed);
let tree = this.physics.add.group()
function generateTree() {
let coordx = Phaser.Math.Between(40, 260)
tree.create(coordx, 120, 'tree')
}
this.time.addEvent({delay: 1000, callback: generateTree, loop: true})
let star = this.physics.add.sprite(240, 620, 'star').setScale(0.2)
star.setInteractive()
star.on('pointerup', function() {
this.scene.physics.moveToObject(star, tree, 1000);
})
With the code above, when you touch the star it disappears and does not move towards the tree. Could you help me?
first, lower your speed…
this.scene.physics.moveToObject(star, tree, 60);
I’m not that experienced with phaser, but you need to add an event in your scene.update to see if the object has reached its target…
var distance = Phaser.Math.Distance.Between(star.x,star.y,tree.x,tree.y);
if (star.speed>0) {
if (distance<10) {star.speed=0;}
}
If you don’t want to use distance, you can use the collide function
I tried and it doesn’t work