如何使用 andEngine 移动精灵的身体?

发布于 2024-11-25 23:48:39 字数 3251 浏览 0 评论 0原文

我想沿着线条的某些部分移动精灵的主体,只是我只能移动精灵,但主体不移动。

public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    final Scene scene = new Scene(2);
    scene.setBackground(new ColorBackground(0.09804f, 0.00274f, 0.0784f));
    this .enableAccelerometerSensor(this );
    this.sPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH,2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH-2, 0,2, CAMERA_HEIGHT);


    PhysicsFactory.createBoxBody(this.sPhysicsWorld, ground,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, roof,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, left,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, right,
            BodyType.StaticBody, wallFixtureDef);
    scene.getFirstChild().attachChild(ground);
    scene.getFirstChild().attachChild(roof);
    scene.getFirstChild().attachChild(left);
    scene.getFirstChild().attachChild(right);

    final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);
    final Body bodyRedBall = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face,
            BodyType.DynamicBody, wallFixtureDef);
    this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, bodyRedBall, true, true));
     scene.attachChild(face);

     final AnimatedSprite face2 = new AnimatedSprite(100, 100, this.mFaceTextureRegion);
     final Body bodyRedBall2 = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face2,
            BodyType.KinematicBody, wallFixtureDef);
     final Path path4 = new Path(3).to(682, 223).to(482, 223).to(682, 223);

     face2.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path4, null, new IPathModifierListener() {
            @Override
            public void onWaypointPassed(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {               
            }
        })));
     this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face2, bodyRedBall2, true, true){
         @Override
         public void onUpdate(final float pSecondsElapsed) {
                 super.onUpdate(pSecondsElapsed);
                 face2.setPosition(face2.getX(), face2.getY());
         }
 });


      scene.attachChild(face2);          
     scene.registerUpdateHandler(this.sPhysicsWorld);  
  return scene;
}

@Override
public void onLoadComplete() {
}

@Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
    // TODO Auto-generated method stub
     final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getY(), pAccelerometerData.getX());
     this.sPhysicsWorld.setGravity(gravity);
     Vector2Pool.recycle(gravity);
}

I want to move body of the sprite along with line in some portion of line, just i able to move sprite only but body is not moving.

public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    final Scene scene = new Scene(2);
    scene.setBackground(new ColorBackground(0.09804f, 0.00274f, 0.0784f));
    this .enableAccelerometerSensor(this );
    this.sPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH,2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH-2, 0,2, CAMERA_HEIGHT);


    PhysicsFactory.createBoxBody(this.sPhysicsWorld, ground,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, roof,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, left,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, right,
            BodyType.StaticBody, wallFixtureDef);
    scene.getFirstChild().attachChild(ground);
    scene.getFirstChild().attachChild(roof);
    scene.getFirstChild().attachChild(left);
    scene.getFirstChild().attachChild(right);

    final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);
    final Body bodyRedBall = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face,
            BodyType.DynamicBody, wallFixtureDef);
    this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, bodyRedBall, true, true));
     scene.attachChild(face);

     final AnimatedSprite face2 = new AnimatedSprite(100, 100, this.mFaceTextureRegion);
     final Body bodyRedBall2 = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face2,
            BodyType.KinematicBody, wallFixtureDef);
     final Path path4 = new Path(3).to(682, 223).to(482, 223).to(682, 223);

     face2.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path4, null, new IPathModifierListener() {
            @Override
            public void onWaypointPassed(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {               
            }
        })));
     this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face2, bodyRedBall2, true, true){
         @Override
         public void onUpdate(final float pSecondsElapsed) {
                 super.onUpdate(pSecondsElapsed);
                 face2.setPosition(face2.getX(), face2.getY());
         }
 });


      scene.attachChild(face2);          
     scene.registerUpdateHandler(this.sPhysicsWorld);  
  return scene;
}

@Override
public void onLoadComplete() {
}

@Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
    // TODO Auto-generated method stub
     final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getY(), pAccelerometerData.getX());
     this.sPhysicsWorld.setGravity(gravity);
     Vector2Pool.recycle(gravity);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无法言说的痛 2024-12-02 23:48:39

你可以使用body.setLinearVelocity();来移动物体,精灵会自动跟随物体移动,因为你已经指定了物理连接器。

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

何时共饮酒 2024-12-02 23:48:39
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());
}
}
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());
}
}
生来就爱笑 2024-12-02 23:48:39

要移动主体必须使用 setTransform

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

并记住您必须将 x 和 y 默认值除以 32

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

私野 2024-12-02 23:48:39

您也可以使用

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

并且您需要除以 32,因为 box2d 不以像素为单位进行操作,因此要使其达到您需要的像素除以 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文