충돌 감지 기능을 개발하려고합니다. 어떤 이유로 LinkedList의 마지막 엔티티에 충돌이 발생하고 있습니다. 콘솔을 사용하여 멈추는 곳을보기 위해 최선을 다했지만, 운이 없었습니다. 다른 모든 엔티티는 작동하지 않으며 마지막 엔티티 만 작동합니다.LinkedList가 모든 항목을 통과하지 않는 이유는 무엇입니까?
public class Player implements Entity {
Image player;
public float x = 100f;
public float y = 100f;
public boolean canGoLeft = true;
public boolean canGoRight = true;
public boolean canGoUp = true;
public 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
if(leftRect.intersects(entity.getRect())) {
canGoLeft = false;
}
if(rightRect.intersects(entity.getRect())) {
canGoRight = false;
}
if(topRect.intersects(entity.getRect())) {
canGoUp = false;
}
if(bottomRect.intersects(entity.getRect())) {
canGoDown = false;
}
if(!leftRect.intersects(entity.getRect())) {
canGoLeft = true;
}
if(!rightRect.intersects(entity.getRect())) {
canGoRight = true;
}
if(!topRect.intersects(entity.getRect())) {
canGoUp = true;
}
if(!bottomRect.intersects(entity.getRect())) {
canGoDown = true;
}
}
public Rectangle getRect() {
return null;
}
가}
이 어떤 문제가 : 여기 코드는?
이것은 작동하지 않습니다. – romofan23
오, 죄송합니다. 당신이하려는 일을 완전히 잘못 해석했습니다. 제 대답을 업데이트하겠습니다. –
@ romofan23 OK 죄송합니다. 다른 답변은 문제가 실제로 맞았습니다. (나는 당신의 목표를 완전히 이해하기 위해 모든 코드를 보지 않았습니다). 문제를 해결해야하는 샘플을 업데이트했습니다. –