2013-12-20 5 views
-1

충돌 감지 기능을 개발하려고합니다. 어떤 이유로 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; 
} 

가}

이 어떤 문제가 : 여기 코드는?

답변

1

왼쪽 및 오른쪽으로 이동할 수 있는지 여부에 대한 전역 값을 설정합니다. 따라서 각 엔티티는 동일한 글로벌 변수를 겹쳐 쓰면서 상태가 마지막 엔티티를 확인한 상태가되도록합니다. 이 시점에서 가장 좋은 방법은 모든 bool이 진실임을 확인한 다음 충돌이 발생할 때만 false로 설정하는 것입니다.

public void update(GameContainer game, int delta) { 
    //... 


    canGoLeft = true; 
    canGoRight = true; 
    canGoDown = true; 
    canGoUp = true; 
    for(Entity entity : Game.entities.entities) { 
     checkCollisions(entity); 
    } 

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; 
    } 
} 

(작동하지 않는 원인이됩니다이 같은 비 교차로의 경우에 true로 canGoDirection를 설정하는 조건의 자신을 제거해야합니다).

+0

이것은 작동하지 않습니다. – romofan23

+0

오, 죄송합니다. 당신이하려는 일을 완전히 잘못 해석했습니다. 제 대답을 업데이트하겠습니다. –

+0

@ romofan23 OK 죄송합니다. 다른 답변은 문제가 실제로 맞았습니다. (나는 당신의 목표를 완전히 이해하기 위해 모든 코드를 보지 않았습니다). 문제를 해결해야하는 샘플을 업데이트했습니다. –

0

매번 부울 값을 덮어 쓰고 있습니다. 대신, 모두를 true으로 설정 한 다음 충돌을 감지 할 때마다 해당 하나를 false으로 표시하십시오.

+0

나는 이해할 수 없다. 더 설명해 주시겠습니까? – romofan23

관련 문제