Lucky Posted May 22, 2014 Share Posted May 22, 2014 If I press down button the sprite starts moving towards 700,700. But if I then press left the sprite just stops and not moving to 100,100.If a leave the velocity.x=0; velocity.y=0; then sprites moves to 100,100 but then starts to move to 700,700 again. How can I get the sprite to got to 100,100 and than stop? velocity.x=0 seems to stop the sprite for ever? /Lucky function update() { if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { attac.troops.s.body.velocity.x=0; attac.troops.s.body.velocity.y=0; s.body.x = 100; s.body.y = 100; }if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { game.physics.arcade.moveToXY(s,700,700,100); }} Link to comment Share on other sites More sharing options...
j0hnskot Posted May 22, 2014 Share Posted May 22, 2014 why don't you use 'moveToXY()' again when pressing the left button?Velocity remains 0 untill you change it again. Link to comment Share on other sites More sharing options...
j0hnskot Posted May 22, 2014 Share Posted May 22, 2014 After some thinking i understand what you want to do. I think at least. Using moveToXY() ,according to docs, is not meant to stop the sprite when it reaches the x and y you entered. It just applies a velocity to reach that place.To make the sprite move there and then stop i think it's better for you to use tweens. Take a look at this example :http://www.html5gamedevs.com/topic/2510-movetoxy-in-conjunction-with-a-mouse-click/ Link to comment Share on other sites More sharing options...
Lucky Posted May 23, 2014 Author Share Posted May 23, 2014 Thanks member but I finaly found a solution.I used s.body.moves function update() { if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { s.body.moves = false; s.x = 200; s.y = 200; } if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { s.body.moves = true; game.physics.arcade.moveToXY(s,700,700,100); }} would be relay helpful and interesting if someone could explain a little moore deeper what this is about Link to comment Share on other sites More sharing options...
lewster32 Posted May 23, 2014 Share Posted May 23, 2014 The physics system is running continuously, trying to make the best predictions about how the object should be moving. This means that if you manually change the position of the object in a non-physical way (i.e. without acceleration or velocity changes) then the physics system gets confused. Setting body.moves to false stops the physics system acting upon the position of the object and allows you to re-position it as you need. If using a physics-enabled sprite, I'd also recommend you use body.x and body.y to adjust the position of the sprite instead of the sprite's own x and y position, as technically the body should be 'driving' the sprite's position when it's enabled. Link to comment Share on other sites More sharing options...
Lucky Posted May 23, 2014 Author Share Posted May 23, 2014 Lewster32 the sprite will not move if I use body.x and body.y when I have turn off the body.movesAnd if I not turn of the body.moves I cant move it but not make it stop Link to comment Share on other sites More sharing options...
j0hnskot Posted May 23, 2014 Share Posted May 23, 2014 You will have problems with collision if you move a sprite with body by relocating the sprite. Change the body.x and body.y before you use body.moves=false. Have you tried it? Link to comment Share on other sites More sharing options...
lewster32 Posted May 23, 2014 Share Posted May 23, 2014 Actually scrap that - I was wrong. When using body.moves = false, you should be using sprite.x and sprite.y, and the body should automatically follow the sprite. Man, I need more coffee. Sorry! Link to comment Share on other sites More sharing options...
Lucky Posted May 23, 2014 Author Share Posted May 23, 2014 It's ok lewster32 I appreciate your help Link to comment Share on other sites More sharing options...
Recommended Posts