Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I use a DigitalOnScreenControl (knob with a four-directional arrow control) to move the entity and the entity which is bound to a physicsHandler.

physicsHandler.setEntity(sprite);
sprite.registerUpdateHandler(physicsHandler);

From the DigitalOnScreenControl, I know which direction I want my sprite to move. Inside its overridden onControlChange function, I call a function animateSprite that checks which direction I chose. Based on the direction, I animate my sprite differently.

Problem: I want to automatically move the sprite to a specific location on the scene, say at coordinates (207, 305). My sprite is at (100, 305, which means it has to move down by 107 pixels.

How do I tell the physicsHandler to move the sprite down by 107 pixels? My animateSprite method will take care of animating the sprite's downward motion.

share|improve this question

4 Answers 4

I'm not familiar with andengine, but it uses box2d so there should be some method similar to:

PhysicsBody.setTransform(float x, float y, float angle)

Which sets the body's position to x, y and rotation to angle.

share|improve this answer
    
For a while, I tried to find a solution in this direction. I am able to setPosition(x,y), but then the sprite jumps. I need a smooth movement. (A bit like a ModeModifier will do.) –  shailenTJ Feb 6 '13 at 12:50

why dont you extend the PhysicsHandler class yourself and add automatic movement to the sprite then call the onUpdate(). For example :

public class Handler extends PhysicsHandler {

public Handler(IEntity pEntity) {
    super(pEntity);

}

@Override
protected void onUpdate(float pSecondsElapsed, IEntity pEntity) {
    // TODO move the pEntity down. eg: pEntity.sety(300)
    super.onUpdate(pSecondsElapsed, pEntity);
}
}
share|improve this answer

Im using this listener to move my sprite around the screen. The "*10" it's like the "velocity".

            @Override 
        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
            playerBody.setLinearVelocity(new Vector2(pValueX*10  , pValueY*10));
        }

Edit: The listener is inside the ScreenControl instance.

share|improve this answer
    
Well this code snippet will not do what I want exactly. I cannot move the sprite from its current position (X,Y) (in pixels) to (X',Y') by supplying the values X'and Y' since I know them. Essentially, the sprite is bound to the DigitalOnScreenControl but I want to move it for the special case where the joystick should not be used to do that. –  shailenTJ Feb 6 '13 at 10:09

I assume you are using Box2D extension from AndEngine. What you are looking for is MouseJoint. There is a working example of MouseJoint in the AndEngineExamples projects that you probably already have. The class you are looking for is PhysicsMouseJointExample.

MouseJoint is used to send the body in continuous movement to a target location. Basically you create MouseJoint as any other joint and then use its method setTarget().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.