wazus Posted May 9, 2016 Share Posted May 9, 2016 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 More sharing options...
rich Posted May 10, 2016 Share Posted May 10, 2016 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. wazus 1 Link to comment Share on other sites More sharing options...
James L Posted June 16, 2016 Share Posted June 16, 2016 Hello wazus, did you get the solution? I have same problem but don't understand Rich's answer. If you've done, would you please share the solution? Link to comment Share on other sites More sharing options...
Recommended Posts