kkaczor Posted June 29, 2014 Share Posted June 29, 2014 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 More sharing options...
lewster32 Posted June 29, 2014 Share Posted June 29, 2014 You should probably read this post: http://www.html5gamedevs.com/topic/7318-create-water-flow-type-physics-when-sprites-overlap/ Your issue in short is that you need to add velocities rather than set them. Link to comment Share on other sites More sharing options...
kkaczor Posted June 29, 2014 Author Share Posted June 29, 2014 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 More sharing options...
lewster32 Posted June 29, 2014 Share Posted June 29, 2014 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 More sharing options...
Recommended Posts