Java 删除body box2d
有人能告诉我为什么返回错误吗 'AL lib:(EE)alc_清理:1个设备未关闭' //鼠标点碰撞回调Java 删除body box2d,java,libgdx,box2d,jbox2d,Java,Libgdx,Box2d,Jbox2d,有人能告诉我为什么返回错误吗 'AL lib:(EE)alc_清理:1个设备未关闭' //鼠标点碰撞回调 private QueryCallback queryCallback = new QueryCallback() { @Override public boolean reportFixture(Fixture fixture) { if(fixture.getBody() == chest){ //add to remove
private QueryCallback queryCallback = new QueryCallback() {
@Override
public boolean reportFixture(Fixture fixture) {
if(fixture.getBody() == chest){
//add to remove list
bodiesToRemove.add(chest);
}
if (fixture.testPoint(tmp.x, tmp.y)){
reportFixture = fixture.getBody();
}
if (!fixture.testPoint(tmp.x, tmp.y))
return false;
//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());
//jointDef.target.set(tmp.x, tmp.y);// initial target point coincide with body anchor
joint = (MouseJoint) world.createJoint(jointDef);// creating the join the physics world
return false;
}
};
//主渲染循环
public void render(float delta) {
// TODO Auto-generated method stub
//code clears the screen with the given RGB colour (black)
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
stage.setCamera(camera);
stage.act(Gdx.graphics.getDeltaTime());
camera.position.x= chest.getPosition().x;
camera.update();
stage.draw();
world.step(1 / 60f, 8, 3);
renderer.render(world, camera.combined);
/////////////////////////////////////////////////////////////////
//这是我试图删除正文时导致错误的代码
if(bodiesToRemove != null){
for(int i = 0; i <bodiesToRemove.size; i++){
Body b = bodiesToRemove.get(i);
if(b != null){
world.destroyBody(b);
b.setUserData(null);
b = null;
}
bodiesToRemove.clear();
}
}
////////////////////////////////////////////////////////////
}
/////////////////////////////////
if(!world.isLocked() && world.getBodyCount() > 0 ){
for(int i = 0; i <bodiesToRemove.size; i++){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeIndex(i);
continue;
}
world.destroyBody(b);
ran = true;
bodiesToRemove.removeIndex(i);
}
}else{
bodiesToRemove.clear();
}
////////////////////////////////////////////////////////////
if(!world.isLocked()&&world.getBodyCount()>0){
对于(int i=0;iBox2D,抛出一些奇怪的错误,这不是其中之一
我假设您使用的是LibGDX,基本上发生的是由于Box2D导致的崩溃(我还假设它会弹出“java或任何停止响应的东西”),您看到的这个错误是在应用程序退出时抛出的,而OpenAL正在关闭流和清理资源。不要太担心这一点
确保在销毁尸体之前检查世界是否已锁定,它通常会抛出一个错误并打印一条明确的消息告诉您这一点,具体取决于您移除尸体的方式
if(!world.isLocked())
// do clean up code here
您不需要清除局部范围内的变量,也不需要清除主体内的userdata作为主体,因为它将不再存在于引用中(除非您将其存储在其他地方)
所以正确的代码应该是这样的:
if(!world.isLocked()){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}
您在迭代过程中也在清除列表,这不是一个好主意
编辑
我能想到的唯一的另一件事是导致这个问题的原因是试图删除一个实际上不在世界身体列表中的身体
请尝试以下操作:
// Ensure world is not locked AND that there is even bodies in the world
if(!world.isLocked() && world.getBodyCount() > 0){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}else{
// There is no bodies in the world, so the bodies in our list are just
// random memory hogs, leave them for the GC by clearing the list
bodiesToRemove.clear();
}
编辑
正如OP所述,问题在于这段代码:
private QueryCallback QueryCallback=new QueryCallback(){
})
更具体地说,这一行:
joint = (MouseJoint) world.createJoint(jointDef);
查询回调在世界步骤期间触发。在Box2D中,您不能在世界步骤期间创建或销毁实体、装置和关节。
你必须在这个方法之外创建它,最简单的方法是通过一个标志。将标志设置为true,并将装置的实例存储在某个地方,然后使用所述标志和装置在游戏循环外部处理它。我想到的第一件事是,它是为了删除正在使用的一个实体,但是看到AL,放弃它,我的question是,box2d在与AL相关时会抛出该错误?虽然逻辑会在应用程序崩溃时发送它,但我想问一下,您是否只抛出该错误“AL lib…”或者由其他人陪同,要知道,never me to lastBox2D不会抛出该错误,实际应用程序会抛出该错误。它是由于崩溃而被抛出的,大多数Box2D崩溃会导致应用程序挂起,然后是CTD,因此AL库没有时间清理。因此它打印此错误。感谢您的回复,我没有想到,看到AL lib放弃他所相信的,原则上哈哈,谢谢你的澄清,我认为你的答案是正确的我删除了我的你可以回答说,你更清楚,你应该在更新世界之前使用你提到的代码或类似的东西,但这只是一个想法,我投票支持upvoteI我已经尝试复制和粘贴你的代码/////////////////////////////////////////////////////////////////如果(!world.isLocked()){for(int i=0;i
@Override
public boolean reportFixture(Fixture fixture) {
if(fixture.getBody() == chest){
//add to remove list
bodiesToRemove.add(chest);
}
if (fixture.testPoint(tmp.x, tmp.y)){
reportFixture = fixture.getBody();
}
if (!fixture.testPoint(tmp.x, tmp.y))
return false;
//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());
//jointDef.target.set(tmp.x, tmp.y);
joint = (MouseJoint) world.createJoint(jointDef);
return false;
}
joint = (MouseJoint) world.createJoint(jointDef);