0
我正在嘗試開發碰撞檢測。出於某種原因,碰撞正在爲LinkedList中的最後一個實體工作。我盡力用控制檯調試它,看看它停止了,但我沒有運氣。所有其他實體都不工作,只有最後一個實體。下面是代碼:LinkedList爲什麼不通過所有的實體?
public class Player implements Entity {
Image player;
public float x = 100f;
public float y = 100f;
boolean canGoLeft = true;
boolean canGoRight = true;
boolean canGoUp = true;
boolean canGoDown = true;
public float speed = 0.15f;
public Rectangle leftRect;
public Rectangle rightRect;
public Rectangle topRect;
public Rectangle bottomRect;
int i = 0;
Entities entities = new Entities();
public Player() {
}
public void update(GameContainer game, int delta) {
if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
if(canGoRight) {
x += speed * delta;
}
}
if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
if(canGoLeft) {
x -= speed * delta;
}
}
if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
if(canGoUp) {
y -= speed * delta;
}
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
if(canGoDown) {
y += speed * delta;
}
}
for(Entity entity : Game.entities.entities) {
checkCollisions(entity);
}
}
public void render(GameContainer game, Graphics g) {
leftRect = new Rectangle(x, y + 5, 2, 80);
rightRect = new Rectangle(x + 45, y + 5, 2, 80);
topRect = new Rectangle(x + 6, y, 36, 2);
bottomRect = new Rectangle(x + 6, y + 90, 36, 2);
//rect = new Rectangle(200, 100, 60, 88);
try {
player = new Image("res/Player.png");
player.setFilter(Image.FILTER_NEAREST);
} catch (SlickException e) {
e.printStackTrace();
}
player.draw(x, y, 60, 88);
//g.draw(leftRect);
//g.draw(rightRect);
//g.draw(topRect);
//g.draw(bottomRect);
}
public void checkCollisions(Entity entity) {
// Collision Detection
canGoLeft = !leftRect.intersects(entity.getRect());
canGoRight = !rightRect.intersects(entity.getRect());
canGoDown = !bottomRect.intersects(entity.getRect());
canGoUp = !topRect.intersects(entity.getRect());
}
public Rectangle getRect() {
return null;
}
}
這是行不通的。當我碰撞時,它阻止了我,但它不會阻止我阻止我這樣做。爲什麼? – romofan23
你可以舉個例子,我很困惑。 – romofan23
我不明白你想說明什麼。你能舉個例子嗎? – romofan23