我正在尝试检测动态和静态身体之间的碰撞。我编写的代码执行没有任何错误,但没有检测到冲突。动态主体是可移动的。当它接触到PhysicsWorld中的其他物体时,没有检测到碰撞。这是我的代码:
public class MainActivity extends BaseGameActivity implements ContactListener {
Body body, bodyr1, bodyr2, bodyr3;
PhysicsWorld pw;
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
scene = new Scene();
final float centerX = (CAMERA_WIDTH - back.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.back.getHeight()) / 2;
Sprite backgr = new Sprite(centerX, centerY, back,
this.getVertexBufferObjectManager());
Sprite rcs1 = new Sprite(85, 250, rc1,
this.getVertexBufferObjectManager());
Sprite rcs2 = new Sprite(195, 240, rc2,
this.getVertexBufferObjectManager());
Sprite rcs3 = new Sprite(295, 250, rc3,
this.getVertexBufferObjectManager());
ans = new AnimatedSprite(86, 245, ttr,
this.getVertexBufferObjectManager()) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
this.setPosition(pX, pY);
super.onManagedUpdate(pSecondsElapsed);
}
};
this.mPhysicsHandler = new PhysicsHandler(ans);
pw = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH),
false);
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(2f,0f, 0.9f);
body = PhysicsFactory.createBoxBody(pw, ans, BodyType.DynamicBody,
objectFixtureDef);
bodyr1 = PhysicsFactory.createBoxBody(pw, rcs1, BodyType.StaticBody,
objectFixtureDef);
bodyr2 = PhysicsFactory.createBoxBody(pw, rcs2, BodyType.StaticBody,
objectFixtureDef);
bodyr3 = PhysicsFactory.createBoxBody(pw, rcs3, BodyType.StaticBody,
objectFixtureDef);
pw.registerPhysicsConnector(new PhysicsConnector(ans, body, true, true));
scene.attachChild(backgr);
scene.attachChild(rcs1);
scene.attachChild(rcs2);
scene.attachChild(rcs3);
scene.attachChild(ans);
body.setUserData("player");
bodyr2.setUserData("3");
pw.setContactListener(this);
scene.registerUpdateHandler(this.mPhysicsHandler);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
@Override
public void beginContact(Contact contact) {
// Fixture x1= contact.getFixtureA();
// Fixture x2=contact.getFixtureB();
Log.i("Contacted","asdasdads");
}
@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
}
}
为什么没有检测到碰撞?请帮忙
答案 0 :(得分:0)
我没有在你班级找到联系人听众。我认为你必须添加以下链接:
mPhysicsWorld.setContactListener(MainActivity.this);