AndEngine 从屏幕上移除物理体
AndEngine remove physics body from screen
你能告诉我我的错误在哪里吗?在 'physicsWorld.destroyBody(this.body)' 行的 Destroy 方法中抛出异常。我正在使用最新版本的 AndEngine 和 Box2D。
public class Asteroid extends Sprite
{
private PhysicsWorld physicsWorld;
private PhysicsConnector physicsConnector;
private Body body;
public Asteroid(float pX, float pY, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager, PhysicsWorld physicsWorld)
{
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.physicsWorld = physicsWorld;
final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(0, 0f, 0f);
mFixtureDef.isSensor = true;
this.body = PhysicsFactory.createCircleBody(physicsWorld, this, BodyDef.BodyType.DynamicBody, mFixtureDef);
this.physicsConnector = new PhysicsConnector(this, this.body, true, true);
physicsWorld.registerPhysicsConnector(this.physicsConnector);
this.body.setLinearVelocity(new Vector2(0, 10));
this.body.setUserData(this);
}
public void Destroy()
{
this.physicsWorld.unregisterPhysicsConnector(this.physicsConnector);
this.body.setActive(false);
physicsWorld.destroyBody(this.body); // throws Exception ???
this.detachSelf();
}
}
您必须等待物理世界循环完成才能对世界进行任何更改。所以最好的方法是创建一个 elementsToBeDestroyed List
,然后在世界循环更新后删除所有物体:
...
physicsWorld = new PhysicsWorld(...) {
@Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
for (Body body : elementsToBeDestroyed) {
destroyBody(body, elementsMap.remove(body).getKey());
}
elementsToBeDestroyed.clear();
}
};
...
private void destroyBody(final Body body, final IShape mask) {
if (physicsWorld != null) {
physicsWorld.unregisterPhysicsConnector(physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(mask));
physicsWorld.destroyBody(body);
}
}
你能告诉我我的错误在哪里吗?在 'physicsWorld.destroyBody(this.body)' 行的 Destroy 方法中抛出异常。我正在使用最新版本的 AndEngine 和 Box2D。
public class Asteroid extends Sprite
{
private PhysicsWorld physicsWorld;
private PhysicsConnector physicsConnector;
private Body body;
public Asteroid(float pX, float pY, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager, PhysicsWorld physicsWorld)
{
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.physicsWorld = physicsWorld;
final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(0, 0f, 0f);
mFixtureDef.isSensor = true;
this.body = PhysicsFactory.createCircleBody(physicsWorld, this, BodyDef.BodyType.DynamicBody, mFixtureDef);
this.physicsConnector = new PhysicsConnector(this, this.body, true, true);
physicsWorld.registerPhysicsConnector(this.physicsConnector);
this.body.setLinearVelocity(new Vector2(0, 10));
this.body.setUserData(this);
}
public void Destroy()
{
this.physicsWorld.unregisterPhysicsConnector(this.physicsConnector);
this.body.setActive(false);
physicsWorld.destroyBody(this.body); // throws Exception ???
this.detachSelf();
}
}
您必须等待物理世界循环完成才能对世界进行任何更改。所以最好的方法是创建一个 elementsToBeDestroyed List
,然后在世界循环更新后删除所有物体:
...
physicsWorld = new PhysicsWorld(...) {
@Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
for (Body body : elementsToBeDestroyed) {
destroyBody(body, elementsMap.remove(body).getKey());
}
elementsToBeDestroyed.clear();
}
};
...
private void destroyBody(final Body body, final IShape mask) {
if (physicsWorld != null) {
physicsWorld.unregisterPhysicsConnector(physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(mask));
physicsWorld.destroyBody(body);
}
}