Search code examples
andengine

AndEngine. Can't make the sprite move using DigitalOnScreenControl


I'm new to AndEngine. I'm trying to move my sprite in PhysicsWorld using DigitalOnScreenControl. But it seems like my sprite cannot move in PhysicsWorld. Without PhysicsWorld the player is controlled correctly, but if I call

this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(myPlayer, body, true, true));

it stops moving. In other words I see the animation like it moves is performed, but it doesn't change its position. Please, advice.

Here is my code: http://pastebin.com/7zE8Jrbg


Solution

  • When I used the DigitalOnScreenControl with PhysicsWorld, I didn't use PhysicsHandler to move my object,

    I mean that I didn't move the object like that:

     physicsHandler.setVelocity(pValueX * 200, pValueY * 200);
    

    however, I did use the body.setLinearVelocity(....) function to move the body the body will move with the sprite connected to it, because you constructed a PhysicsConnector between your sprite and your body, using the line :

     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(myPlayer, body, true, true));
    

    so whenever you move your body, your sprite will be moved too.