Box2D/AndEngine - ContactListener for multiple object classes

拥有回忆 提交于 2019-12-25 01:16:05

问题


I'm developing a little Android game in Java, using AndEngine for graphics and Box2D for physics - specifically, collision handling. I have some different types of objects with constructors in classes, like so:

MainActivity.java
Enemy.java
Npc.java
Door.java

I have a static PhysicsWorld in the main class, and I was setting up a ContactListener from the Enemy class, to define what happens when one of the enemies hits something. However, I tried to set up another ContactListener for the Door class, when I discovered that each PhysicsWorld has only one ContactListener.

Essentially, my question is this: what is the best way to get around this?

I'm aware I've probably explained this rather badly, so my apologies.


回答1:


You can use your single ContactListener to manage the entire world; Contact.getFixtureA/B() will return the fixtures involved in the contact. You can utilize Fixture.getBody() to get the associated Body with each collision fixture; if, for example, your Door and Enemy objects are associated with the Bodys as user data, then you can use Body.getUserData() to retrieve that.



来源:https://stackoverflow.com/questions/11835981/box2d-andengine-contactlistener-for-multiple-object-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!