我有一个动态精灵和几个静态精灵。我的动态身体:
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(180, 20);
bodyMonkey = world.createBody(bodyDef);
PolygonShape abcBox = new PolygonShape();
bodyMonkey.setUserData(monkey1);
bodyMonkey.setFixedRotation(true);
abcBox.setAsBox(10.0f, 10.0f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = abcBox;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.9f;
Fixture fixture = bodyMonkey.createFixture(fixtureDef);
abcBox.dispose();
bodyMonkey.setLinearVelocity(new Vector2(1f, 0.5f));
bodyMonkey.setLinearDamping(1.0f);
我必须实现 ContactListener。我的课:
@Override
public void beginContact(Contact contact) {
Object a = contact.getFixtureA().getBody().getUserData();
Object b = contact.getFixtureB().getBody().getUserData();// null
Gdx.app.log("1", ""+a);
Gdx.app.log("2", ""+b);
if(a!=null&&b!=null) {
Gdx.app.log("ok", "");
screen2dBox.restartGame();
}
}
@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}
但是对象 b 始终为空(a 不,b 是动态的,我在日志中看到了它)。我曾尝试使用 try、catch、finally 来克服这个问题(在 finally 块中 - screen2dBox.restartGame();),但我遇到错误,即联系人不能为空。不知道怎么解决?感谢帮助。
静态体:
BodyDef bodyDefPlatform1 = new BodyDef();
bodyDefPlatform1.type = BodyType.StaticBody;
bodyDefPlatform1.position.set(50, 280);
bodyPlatform1 = world.createBody(bodyDefPlatform1 );
PolygonShape platformBox1 = new PolygonShape();
bodyPlatform1.setUserData(platform);
platformBox1.setAsBox(20.0f, 10.0f);
FixtureDef fixtureDefPlatform1 = new FixtureDef();
fixtureDefPlatform1.shape = platformBox1;
Fixture fixturePlatform = bodyPlatform1.createFixture( fixtureDefPlatform1);
platformBox1.dispose();