我正在创建自己的手机游戏:2D图形,由士兵移动和射击。我遇到了问题。我有两个TiledLayer。首先是地面,第二个包含墙壁,树木等。如何检测放置在2nd TiledLayer上的这些物体的碰撞?我试过了:如果发生碰撞继续向后移动。这是不对的。我正在前进,这里是墙:按UP - 好吧,士兵走了。但是当按下LEFT士兵时,尽管它应该左转(我知道因此已经编写了代码)。
然而,它是如何写的?
public void run() {
while(true) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
soldier.setTransform(Sprite.TRANS_ROT270);
if(!(soldier.collidesWith(traps, false))) {
soldier.move(-10, 0);
} else {
if((keyState & LEFT_PRESSED) != 0) soldier.move(10, 0);
}
} else if ((keyState & UP_PRESSED) != 0) {
soldier.setTransform(Sprite.TRANS_NONE);
if(!(soldier.collidesWith(traps, false))) {
soldier.move(0, -10);
} else {
if((keyState & UP_PRESSED) != 0) soldier.move(0, 10);
}
} else if ((keyState & DOWN_PRESSED) != 0) {
soldier.setTransform(Sprite.TRANS_ROT180);
if(!(soldier.collidesWith(traps, false))) {
soldier.move(0, 10);
} else {
if((keyState & DOWN_PRESSED) != 0) soldier.move(0, -10);
}
} else if ((keyState & RIGHT_PRESSED) != 0) {
soldier.setTransform(Sprite.TRANS_ROT90);
if(!(soldier.collidesWith(traps, false))) {
soldier.move(10, 0);
} else {
if((keyState & RIGHT_PRESSED) != 0) soldier.move(-10, 0);
}
} else if((keyState & FIRE_PRESSED) != 0) {
createBullet();
}
答案 0 :(得分:1)
考虑simplifying your code并添加日志记录语句,以便更轻松地调试您描述的问题。
......怎么写?
以下是关于我如何......
...在the code snippet provided in question的特定角落出现了什么问题:
public void run() {
while (true) {
// extract method:
handle(getKeyStates(), soldier.collidesWith(traps, false));
}
}
private void log(String msg) { System.out.println(msg); } // simplify logging
private void moveSoldier(int dx, int dy) {
// extract method to simplify logging
System.out.println("move soldier, dx = [" + dx + "], dy = [" + dy + "]");
soldier.move(dx, dy);
}
private void handle(int keyState, boolean soldierCollidesWithTraps) {
// IMPL NOTE below code could (should) be refactored further
if ((keyState & LEFT_PRESSED) != 0) {
// IMPL NOTE consider extracting this block into dedicated method
log("left pressed"); // log useful information
soldier.setTransform(Sprite.TRANS_ROT270);
if (!soldierCollidesWithTraps) {
log("no collision");
moveSoldier(-10, 0);
return; // return as soon as we think we're done
}
// because of 'return' above, 'else' here is not needed anymore
if ((keyState & LEFT_PRESSED) != 0) {
log("left pressed - why re-check it here?");
moveSoldier(10, 0);
return;
}
log("no move"); // IMPL NOTE seems to be bug in your code logic above
return;
}
if ((keyState & UP_PRESSED) != 0) {
log("up pressed");
soldier.setTransform(Sprite.TRANS_NONE);
if (!soldierCollidesWithTraps) {
log("no collision");
moveSoldier(0, -10);
return;
}
if ((keyState & UP_PRESSED) != 0) {
log("up pressed - why re-check it here?");
moveSoldier(0, 10);
return;
}
log("no move"); // IMPL NOTE seems to be bug in your code logic above
return;
}
if ((keyState & DOWN_PRESSED) != 0) {
log("down pressed");
soldier.setTransform(Sprite.TRANS_ROT180);
if (!soldierCollidesWithTraps) {
log("no collision");
moveSoldier(0, 10);
return;
}
if ((keyState & DOWN_PRESSED) != 0) {
log("down pressed - why re-check it here?");
moveSoldier(0, -10);
return;
}
log("no move"); // IMPL NOTE seems to be bug in your code logic above
return;
}
if ((keyState & RIGHT_PRESSED) != 0) {
log("right pressed");
soldier.setTransform(Sprite.TRANS_ROT90);
if (!soldierCollidesWithTraps) {
log("no collision");
moveSoldier(10, 0);
return;
}
if ((keyState & RIGHT_PRESSED) != 0) {
log("right pressed - why re-check it here?");
moveSoldier(-10, 0);
return;
}
log("no move"); // IMPL NOTE seems to be bug in your code logic above
return;
}
if ((keyState & FIRE_PRESSED) != 0) {
log("fire pressed");
createBullet();
return;
}
log("no action");
}