[英]Box2D/AndEngine - ContactListener for multiple object classes
I'm developing a little Android game in Java, using AndEngine for graphics and Box2D for physics - specifically, collision handling. 我正在用Java开发一个小型Android游戏,使用AndEngine进行图形处理,使用Box2D进行物理处理-特别是碰撞处理。 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. 我在主类中有一个静态PhysicsWorld,并且正在从Enemy类中设置一个ContactListener,以定义当敌人之一击中某物时发生的情况。 However, I tried to set up another ContactListener for the Door class, when I discovered that each PhysicsWorld has only one ContactListener.
但是,当我发现每个PhysicsWorld只有一个ContactListener时,我尝试为Door类设置另一个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. 我知道我可能已经对此做出了非常糟糕的解释,因此我深表歉意。
You can use your single ContactListener
to manage the entire world; 您可以使用单个
ContactListener
来管理整个世界。 Contact.getFixtureA/B()
will return the fixtures involved in the contact. Contact.getFixtureA/B()
将返回联系人所涉及的固定装置。 You can utilize Fixture.getBody()
to get the associated Body
with each collision fixture; 您可以利用
Fixture.getBody()
来获取每个碰撞夹具的关联Body
; if, for example, your Door
and Enemy
objects are associated with the Body
s as user data, then you can use Body.getUserData()
to retrieve that. 例如,如果您的
Door
和Enemy
对象作为用户数据与Body
关联,则可以使用Body.getUserData()
进行检索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.