Jump to content

velocity = 0 seems to stop the sprite for ever


Lucky
 Share

Recommended Posts

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

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

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

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

 Share

  • Recently Browsing   0 members

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