Jump to content

Adding force to a moving sprite


kkaczor
 Share

Recommended Posts

Hi!

I have got sprite which is moving with constant speed using: moveLeft  Now I want to apply force to it to simulate effect of pushing it away from player. I tried:

zombie.sprite.body.velocity.x = -100;
zombie.sprite.body.velocity.y = -100;
but the x component seems to be ignored - I think that i because of moveLeft in another part of a update loop. How I suppose to deal with it?
 
Link to comment
Share on other sites

Thank you for your response but i am stil not able to deal with this problem. I don't know how should I move object with constant speed and be able to set sprite.body.force. Corrently object is only moving on y axix (beacuse i am not moving it with constant speed on that axix).

Link to comment
Share on other sites

You're setting a constant force with moveLeft, which means any other attempts to change the speed will fail as this force is, predictably, constant. In the post I linked, I suggested having an externalForce property which is added to whatever constant force is being applied.This is easier done by sticking to velocity rather than using moveLeft, as the maths becomes clearer:

var speed = 100; // move right at 100 pixels per secondvar force = -50; // apply an extra force left at 50 px/sfunction update() {  sprite.body.velocity.x = speed + force; // combined velocity is now right at 50px/s}

Obviously if your speed is -100 (moving to the left) the added force will mean the sprite goes left at 150px/s, so this starts to behave naturally, with forces adding or reducing the existing velocity depending on which direction the force is directed and which direction the object is moving in.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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