gpt4 book ai didi

android - 在碰撞 JBox2D android 上销毁 body

转载 作者:行者123 更新时间:2023-11-29 00:21:18 25 4
gpt4 key购买 nike

我需要在 JBox2D android 游戏中碰撞后摧毁一具尸体。我发现当 body 接触时,JBox2D 世界会被锁定。我想在碰撞后摧毁其中一具尸体。世界解锁后我能接到任何回电吗?我找到了一个选项,可以将 body 添加到数组中以销毁它。但是什么时候摧毁 body ?。我没有在这个游戏中使用 andengine/libgdx。在下面找到我的碰撞监听器类,

private class CollisionListener implements ContactListener {

@Override
public void beginContact(Contact contact) {
Object fixtureA = contact.getFixtureA().getUserData();
Object fixtureB = contact.getFixtureB().getUserData();
Body mBodyA = contact.getFixtureA().getBody();
Body mBodyB = contact.getFixtureB().getBody();
if (fixtureA instanceof Bullet) {
destroyBalloonBody(mBodyB);
}
if (fixtureB instanceof Bullet) {
destroyBalloonBody(mBodyA);
}

}

@Override
public void endContact(Contact contact) {

}

@Override
public void postSolve(Contact contact, ContactImpulse contactImpulse) {

}

@Override
public void preSolve(Contact contact, Manifold manifold) {

}
}

public void destroyBalloonBody(Body balloon){

//Can i start a new thread which is having a loop until the world become release for destroying the body
//Or
//Do i need to add the body to a deletionArrayList to destroy it.

}

最佳答案

我认为问题在于您正试图在碰撞回调期间移除主体。

来自 Box2D 手册:

It is tempting to implement game logic that alters the physics world inside a contact callback. For example, you may have a collision that applies damage and try to destroy the associated actor and its rigid body. However, Box2D does not allow you to alter the physics world inside a callback because you might destroy objects that Box2D is currently processing, leading to orphaned pointers.

也就是说,你不应该在碰撞回调中销毁物体。

您应该存储对您希望销毁的物体的引用,并在更新周期后使用世界引用销毁它们。

这是您需要的吗?

关于android - 在碰撞 JBox2D android 上销毁 body ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com