感谢您的时间。
我正在通过libGDX使用Box2D创建一个Pong克隆。当球体与两个目标传感器主体之一(下图)接触时试图删除球体时,我有一个粘滞点导致空指针异常。
我想将接触的Ball体添加到列表中,以便稍后我可以遍历列表以删除Ball体(以及将来的多个Ball体)。
堆栈跟踪:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.physics.box2d.World.destroyBody(World.java:311)
at com.ckq3r.Ponger.screens.GameScreen.update(GameScreen.java:484)
at com.ckq3r.Ponger.screens.GameScreen.render(GameScreen.java:114)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.ckq3r.Ponger.PongerGame.render(PongerGame.java:236)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)
我的GameScreen类中的第484行是world.destroyBody(circleBody);
行,它位于下面显示的if(scoredGoal1 == true){stuff}
方法的public void render(float delta){stuff}
条件语句中。
public class GameScreen implements Screen, InputProcessor{
/*----methods and variables omitted for readability------*/
private boolean scoredGoal1 = false, scoredGoal2 = false;
ArrayList<Body> ballDeletionList = new ArrayList<Body>();
/*==============Screen implementation methods============*/
@Override
public void show(){
/*ball*/
BodyDef circleDef = new BodyDef();
Body circleBody = world.createBody(circleDef);
circleBody.setUserData(1);
CircleShape circleShape = new CircleShape();
FixtureDef circleFixture = new FixtureDef();
circleFixture.shape = circleShape;
circleBody.createFixture(circleFixture);
circleShape.dispose();
}
@Override
public void render(float delta) {
world.step(Gdx.app.getGraphics().getDeltaTime(), 8, 3);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
debugRenderer.render(world, camera.combined);
/*-------ball deletion experiment-------*/
if(scoredGoal1 == true){
//iterate through ballDeletionList somehow?
world.destroyBody(circleBody);
circleBody.setUserData(null);
circleBody = null;
scoredGoal1 = false;
//clear ballDeletionList
}else if(scoredGoal2 == true){
//iterate through ballDeletionList somehow?
world.destroyBody(circleBody);
circleBody.setUserData(null);
circleBody = null;
scoredGoal2 = false;
//clear ballDeletionList
}
/*-----end ball deletion experiment------*/
}
/*===========Box2D contact listener=============*/
private void createContactListener() {
world.setContactListener(new ContactListener() {
@Override
public void beginContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Gdx.app.log("beginContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(2) || fixtureA.getBody().getUserData().equals(2) && fixtureB.getBody().getUserData().equals(1)){
Gdx.app.log("HIT", "goal1 contact");
/*ball deletion experiment*/
ballDeletionList.add(circleBody);
Gdx.app.log("Ball", "circleBody added to deletion list");
scoredGoal1 = true;
/*ball deletion experiment*/
}
if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(3) || fixtureA.getBody().getUserData().equals(3) && fixtureB.getBody().getUserData().equals(1)){
Gdx.app.log("HIT", "goal2 contact");
/*ball deletion experiment*/
ballDeletionList.add(circleBody);
Gdx.app.log("Ball", "circleBody added to deletion list");
scoredGoal2 = true;
/*ball deletion experiment*/
}
}
});
我的逻辑如下:球体和目标传感器身体之间的接触:
ArrayList<Body> ballDeletionList = new ArrayList<Body>();
,scoredGoal1 = true;
布尔标志将设置为true。render();
检查scoredGoal1 = true
或scoredGoal2 = true
,然后在world.step()
方法后删除适用的正文/机构。我在整个网络中搜索过其他代码示例和教程,但却找不到含糊不清的答案,因为代码是专用的,或者我目前只了解Java。
如果可以发布Java / libGDX代码示例解决方案,那就太好了。
答案 0 :(得分:2)
您无法删除contactlistener中的实体,因为它位于您的世界步骤中,并且世界已被锁定。 我在你的渲染方法中完成了与你想做的完全相同的事情:
if(ballDeletionList.size>0) ballDeletionList.clear();
此外,使用libgdx建议使用Array http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Array.html而不是ArrayList,它会产生更少的垃圾并进行更多优化。