我几乎解决了如何处理碰撞问题。但我被困在一个错误上。我看着图书馆,我还不明白如何解决这个错误。我的代码如下所示。
final Array destructableBodies = new Array();
final Body destroyer2 = destroyBody;
final Array<Body> bodiesToDestroy = new Array<Body>();
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
System.out.println(delete);
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
if (delete = true)
{
for(Body body : destructableBodies)
{
if (delete = true)
{
System.out.println("ENTRY POINT 7");
delete = false;
world.destroyBody(body);
}
break;
}
System.out.println("ENTRY POINT 8");
}
bodiesToDestroy.clear();
//在渲染方法
中world.setContactListener(new ContactListener(){
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
Body bodyA = contact.getFixtureA().getBody();
Body bodyB = contact.getFixtureB().getBody();
// you can also use the user data to store information about the destructibility of bodies
if(bodyA == destroyer2 && destructableBodies.contains(bodyB, true))
{
bodiesToDestroy.add(bodyB);
System.out.println("ENTRY POINT 2");
delete = true;
}
else if(bodyA == destroyer2 && destructableBodies.contains(bodyA, true))
{
bodiesToDestroy.add(bodyA);
System.out.println("ENTRY POINT 3");
delete = true;
}
destructableBodies.add(destroyBody);
System.out.println("ENTRY POINT 4");
}
@Override
public void postSolve(Contact contact, ContactImpulse arg1) {
}
@Override
public void endContact(Contact arg0) {
System.out.println("ENTRY POINT 5");
reload();
delete = false;
}
@Override
public void beginContact(Contact arg0) {
System.out.println("ENTRY POINT 6");
}
});
我已经更改了代码并且正文被破坏但我的游戏崩溃了,我得到断言失败错误。我也得到了身体数量&lt;身体容量。我设置了一些打印语句,发现每次发生碰撞时,它都会为每个循环输入两次并删除一个主体两次,但只创建一个主体。不知道该怎么做我甚至设置了布尔标志,但即使这样也行不通。