1

使用 AndEngine,我想移动一个对象,所以我创建了一个 Sprite:

tower1 = new Sprite(50, 150, this.tower1TextureRegion,
            this.getVertexBufferObjectManager());
tower1Body = PhysicsFactory.createBoxBody(physicsWorld, tower1,
            BodyType.KinematicBody, towerFixtureDef);
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(tower1,
            tower1Body, true, true));


然后定义移动:

moveTower1 = new MoveXModifier(actualDuration, tower1.getX(), -150f)
     {
     @Override protected void onModifierFinished(IEntity 
     removeTower(tower1); }
     }; 
     tower1.registerEntityModifier(moveTower1);

当我运行这个应用程序时,精灵在移动,但物理对象没有。我做错了什么?

4

2 回答 2

1

使用 box2d,您需要移动身体,而不是精灵。你应该把精灵想象成一个木偶,由身体控制。

于 2013-02-23T18:29:55.363 回答
0

如果将精灵附加到身体上,则不能再使用修改器移动它。物理世界现在在位置方面基本上拥有它(即,如果身体由于施加的力而移动,则连接的精灵将随之移动)。您只能通过对身体本身施加力来移动它。即,

tower1Body.applyLinearImpulse(impulseX, impulseY, pointX, pointY);

或者,

tower1Body.applyForce(forceX, forceY, pointX, pointY);

或线速度,

tower1Body.setLinearVelocity(vX, vY);

Also be warned that the co ordinates in the physics world differ from those in your scene. For example tower1.getX() are the coordinates in your scene of sprite tower1 but in the physics world the position of the body will differ by /PIXEL_TO_METER_RATIO_DEFAULT.

Hope this helps.

于 2013-02-23T18:30:28.830 回答