how to move body of sprite using andEngine?

久未见 提交于 2019-12-04 15:01:55
chrypthic

You can use body.setLinearVelocity(); to move the body, the sprite will automatically follow the body because you have specified a physics connector.

public class MoveBodyModifier extends MoveModifier {

private Body mBody;

public MoveBodyModifier(float pDuration, float pFromX, float pToX, float pFromY, float pToY, Body body) {
    super(pDuration, pFromX, pToX, pFromY, pToY);
    mBody = body;
}

@Override
protected void onSetInitialValues(IEntity pEntity, float pX, float pY) {
    mBody.setTransform(pX/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT,
            pY/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, mBody.getAngle());
}

@Override
protected void onSetValues(IEntity pEntity, float pPercentageDone, float pX, float pY) {
    mBody.setTransform(pX/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT,
            pY/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, mBody.getAngle());
}
}

to move the body must use the setTransform

yourBody.setTransform(new Vector2(x/32,y/32), 0); 

and remember that you have to divide x and y default by 32

you can also go

yourBody.setTransform(x/32,y/32), 0);

and you need to divide by 32 because box2d does not operates in pixels so to get it to pixels you need to divide by 32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!