我遇到了一些麻烦,似乎一些getContactList
正在返回的联系人实际上并不是联系人。
相关代码:
List < Contact > contacts = theBall.body.getWorld().getContactList(); //get all contacts in current world
if (contacts.size() == 0) selected = -1; // if there are no contacts, set selected to -1, meaning no menu item is selected
/* loop through all contacts */
for (Contact con: contacts) {
Fixture fixtureA = con.getFixtureA();
Fixture fixtureB = con.getFixtureB();
LogapPlatform lplat = LogapUtils.cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapPlatform.class);
LogapBall lball = LogapUtils.cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapBall.class);
/* check if the contact is between a LogapBall object and a LogapPlatform object */
if (lball != null && lplat != null) {
/* if true, determine which of the five menu platforms the ball has been placed on top of */
for (int i = 0; i < LogapLevel.ctrPlat.size(); i++) {
if (lplat.equals(LogapLevel.ctrPlat.get(i)) && (lball.getPos().y > lplat.getPos().y)) {
selected = (int) Math.floor(i / 5); // set selected to the "id" of the selected menu platform
}
}
} else {
selected = -1; // if false, set selected to -1
}
}
/* "NEW GAME" is the only menu item currently implemented. its "id" = 2 */
if (selected == 2)
LogapGame.font.setColor(Color.YELLOW); // if the ball is placed on top of the menu platform for NEW GAME, set the font color of the text to yellow
else
LogapGame.font.setColor(Color.PINK); // else, keep the text pink
/* draw the text on the screen */
if (logLev.menuLevel) {
LogapGame.font.draw(batch, "NEW GAME", 135, 150);
LogapGame.font.setColor(LogapGame.GRAY_192);
}
要查看此代码的实际运行情况(以及我遇到的麻烦),这里有一个视频示例来说明(顺便说一句,我启用了 Box2DDebugRenderer):
http://www.youtube.com/watch?v=RcHwt4b4lt0
因此,正如您可能已经看到的那样,我遇到的问题是即使球尚未与平台接触,文本也会变成黄色。应该发生的是,只有当球与平台接触时,文本才会变成黄色。如果有任何疑问,他们是否实际上没有联系,这里有一个特写:
示例 http://static.rateyourmusic.com/lk/l/w/02413fc8b4aef5d9f37c3b12420d4a2d/4870735.png
即使两个对象尚未接触,为什么文本会改变颜色的任何想法?
PS - 对于任何想知道为什么我不使用碰撞侦听器的人,我是。只是不是为了这个特定的逻辑。我遇到了碰撞侦听器的一些怪癖,所以我决定对于游戏中对象的某些行为,我宁愿像这样手动查询联系人。