Java 为什么不是';LinkedList没有遍历所有实体吗?

Java 为什么不是';LinkedList没有遍历所有实体吗?,java,collision,Java,Collision,我正在尝试开发碰撞检测。由于某些原因,碰撞正在为LinkedList中的最后一个实体工作。我已经尽了最大努力使用控制台调试它,看看它停在哪里,但我没有运气。所有其他实体都不工作,只有最后一个实体工作。代码如下: public class Player implements Entity { Image player; public float x = 100f; public float y = 100f; boolean canGoLeft = true; boolean canGoRi

我正在尝试开发碰撞检测。由于某些原因,碰撞正在为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;
}

}

它正在迭代整个列表,但每次调用时,checkCollisions都会覆盖canGoLeft、canGoRright等的值

你应该这样做

canGoLeft = canGoLeft && !leftRect.intersects(entity.getRect());

它正在迭代整个列表,但每次调用时,checkCollisions都会覆盖canGoLeft、canGoRright等的值

你应该这样做

canGoLeft = canGoLeft && !leftRect.intersects(entity.getRect());

它正在迭代整个列表,但每次调用时,checkCollisions都会覆盖canGoLeft、canGoRright等的值

你应该这样做

canGoLeft = canGoLeft && !leftRect.intersects(entity.getRect());

它正在迭代整个列表,但每次调用时,checkCollisions都会覆盖canGoLeft、canGoRright等的值

你应该这样做

canGoLeft = canGoLeft && !leftRect.intersects(entity.getRect());


这是行不通的。当我碰撞时,它会阻止我,但它不会阻止我朝那个方向走。为什么?你能举个例子吗?我很困惑。我不明白你想解释什么。你能举个例子吗?假设你已经实现了JVMATL建议的改变(顺便说一句,我同意),听起来好像你说的是,在转弯N时,你试图向左走,而canGoLeft在某个时候告诉你“我不能再向左走了”。然后你向右走一两次,然后再尝试向左走,但因为canGoLeft从未重置,你再也不能向左走了。很抱歉,这听起来让人困惑,但我发现了几个问题:1)如果移动到不同的方向,则需要重置变量;2)在更新x或y之前,您可能应该检查是否可以移动到某个方向。@shoover是正确的。我认为在进入checkCollision for()循环之前,需要将canGoDown/up/etc重置为true。这不起作用。当我碰撞时,它会阻止我,但它不会阻止我朝那个方向走。为什么?你能举个例子吗?我很困惑。我不明白你想解释什么。你能举个例子吗?假设你已经实现了JVMATL建议的改变(顺便说一句,我同意),听起来好像你说的是,在转弯N时,你试图向左走,而canGoLeft在某个时候告诉你“我不能再向左走了”。然后你向右走一两次,然后再尝试向左走,但因为canGoLeft从未重置,你再也不能向左走了。很抱歉,这听起来让人困惑,但我发现了几个问题:1)如果移动到不同的方向,则需要重置变量;2)在更新x或y之前,您可能应该检查是否可以移动到某个方向。@shoover是正确的。我认为在进入checkCollision for()循环之前,需要将canGoDown/up/etc重置为true。这不起作用。当我碰撞时,它会阻止我,但它不会阻止我朝那个方向走。为什么?你能举个例子吗?我很困惑。我不明白你想解释什么。你能举个例子吗?假设你已经实现了JVMATL建议的改变(顺便说一句,我同意),听起来好像你说的是,在转弯N时,你试图向左走,而canGoLeft在某个时候告诉你“我不能再向左走了”。然后你向右走一两次,然后再尝试向左走,但因为canGoLeft从未重置,你再也不能向左走了。很抱歉,这听起来让人困惑,但我发现了几个问题:1)如果移动到不同的方向,则需要重置变量;2)在更新x或y之前,您可能应该检查是否可以移动到某个方向。@shoover是正确的。我认为在进入checkCollision for()循环之前,需要将canGoDown/up/etc重置为true。这不起作用。当我碰撞时,它会阻止我,但它不会阻止我朝那个方向走。为什么?你能举个例子吗?我很困惑。我不明白你想解释什么。你能举个例子吗?假设你已经实现了JVMATL建议的改变(顺便说一句,我同意),听起来好像你说的是,在转弯N时,你试图向左走,而canGoLeft在某个时候告诉你“我不能再向左走了”。然后你向右走一两次,然后再尝试向左走,但因为canGoLeft从未重置,你再也不能向左走了。很抱歉,这听起来让人困惑,但我发现了几个问题:1)如果移动到不同的方向,则需要重置变量;2)在更新x或y之前,您可能应该检查是否可以移动到某个方向。@shoover是正确的。我认为在进入checkCollision for()循环之前,需要将canGoDown/up/etc重置为true。