Jump to content

Problems with game.physics.arcade.moveToXY()


wazus
 Share

Recommended Posts

I have directions stored in the command_array  and simply want the sprite (this.player) to move to the right.

I am using the function moveToXY() like this:

  for (var i = 0; i < this.command_array.length; i++) {

            if (this.command_array[i].key === 'walk_right_com') {
               
                    this.game.physics.arcade.moveToXY(
                    this.player, //sprite reference 
                    this.player.body.x + 150, // target x position
                    Phaser.Math.snapTo(this.player.body.y, 70), 100 
                );                
            }
  }

The player moves to the right but does not stop and keeps moving beyond the target (+150).

I then read a suggestion to add:

this.player.body.velocity.x = 0;
this.player.body.velocity.y = 0;

to my update function which would stop it from dragging beyond its target. BUT this stops the sprite from moving after just a tiny tiny movement to the right no matter what value I insert after this.player.body.x + ... .

My thinking is that the update loop is so fast that it stops the sprite before it can finish its complete movement.

Any advice would be greatly appreciated since I am very new to using phaser.

Thanks!

Link to comment
Share on other sites

From 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.
    * Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
    * Note: The display object doesn't stop moving once it reaches the destination coordinates.
    * Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
*/

re: the second "Note:"

If nothing else is impacting on the speed of the sprite, you could set a Timer to stop its velocity after the maxTime has elapsed.

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

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