Jump to content

moveToXY doesn't stop even when reached destination

James L

Recommended Posts

Hi, I use function moveToXY to move a sprite to coordinates. However it doesn't stop when reached the destination. It stops only when collide something.

For example, sprite is at coordinates 160/160. Code is game.physics.arcade.moveToXY(sprite, 126, 160, speed);  It moves to 32/160 and stops when collided a wall sprite at 32/160.

How to stop it at specified coordinates?


I see the same problem here 

But I don't understand the answer.
Please advice me. Thanks.

Link to comment
Share on other sites

What it says in the other thread is that the moveToXY does not stop the movement automatically when it reaches the position. You need to stop it yourself once it reaches the target position.

As listed in the docs:



"Move the given display object towards the x/y coordinates at a steady velocity. If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds. Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms."

So basically the solution of this is to add a max time to your command e.g. game.physics.arcade.moveToXY(sprite, 126, 160, speed,5000); which means the object will arrive in 5 seconds (input is in miliseconds). When you start the moveToXY you also add a timer with the same delay as you set in moveToXY which will then stop the object (which should after those same 5 seconds be on your desired location), something like this:

game.time.events.add(5000, function () {
   //Set object speed to zero here.
}, this);

This will probably not 100% perfect due to the 50ms variance, but you might can fix that by forcing the object to that particular position the moment its speed is set to 0.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...