我正在尝试使IF &&循环冲突检测正常工作。但是,它不断撞到不存在的墙壁。因此,请仔细阅读我的代码,并告诉我逻辑错误在哪里,因为我找不到它。
我举一个例子,说Y字母是我的角色,X字母是墙壁,地板和屋顶。
XXXXXXXX X X X Y X X X XXXXXXXX
我的角色将无法跳跃或受到重力的影响。由于某种原因,碰撞检测超出了放置瓷砖的位置。
public void collisionDetection(){
for (int i = 0; i <tilesRect.length; i++) {
for (int a = 0; a < tilesRect[i].length; a++) {
if ( whereToDraw.right == tilesRect[i][a].left && whereToDraw.bottom > tilesRect[i][a].top && whereToDraw.top < tilesRect[i][a].bottom ){
//code for death here
//this is for running into tile
noCollision = false;
}
if (whereToDraw.top == tilesRect[i][a].bottom && whereToDraw.left < tilesRect[i][a].right && whereToDraw.right > tilesRect[i][a].left){
//jumping into tile
jumping = false;//not working don't know why
gravityOn = true;
noCollision = false;
}
if (whereToDraw.bottom == tilesRect[i][a].top && whereToDraw.left < tilesRect[i][a].right && whereToDraw.right > tilesRect[i][a].left){
gravityOn = false;
//landed on tile
noCollision = false;
}
}
}
if (noCollision){
if (jumping != true){
gravityOn = true;
// gravityMethod();
}
}
}