Box2D的/ AndEngine - ContactListener多个对象类 [英] Box2D/AndEngine - ContactListener for multiple object classes
问题描述
我正在开发在Java中一个小的Android游戏,使用AndEngine图形和Box2D的物理学 - 具体而言,冲突处理。我有一些不同类型的对象,在类的构造函数,像这样:
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
我在主类中的静态PhysicsWorld,我被设置从敌人类ContactListener,确定会发生什么,当敌人之一击中的东西。不过,我试图建立另一个ContactListener的大门类,当我发现每个PhysicsWorld只有一个ContactListener。
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.
推荐答案
您可以使用单一的 ContactListener
来管理整个世界; Contact.getFixtureA / B()
将返回参与接触的装置。您可以利用 Fixture.getBody()
来获得相关的机身
每次碰撞夹具;如果,例如,你的门
和敌人
对象与正文相关
译文]用户数据,那么你可以使用 Body.getUserData()
来检索。
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 Body
s as user data, then you can use Body.getUserData()
to retrieve that.
这篇关于Box2D的/ AndEngine - ContactListener多个对象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!