Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';LinkedList是否会遍历所有实体?_Java_Collision Detection_Collision - Fatal编程技术网

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

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

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


这有什么问题?

每次都会覆盖布尔值。相反,请事先将它们全部设置为
true
,然后在每次检测到碰撞时标记相应的
false

您正在设置全局值,以确定是否可以左右上下移动。因此,每个实体都在覆盖相同的全局变量,使其状态处于上次检查的实体的状态。我认为这一点上最好的方法是确保所有bool开始为true,然后只在发生碰撞时将它们设置为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;
    }
}

(在非交叉口的情况下,一定要排除将
canGoDirection
设置为true的条件,因为这会导致它不起作用)。

我不明白。你能再解释一下吗?哦,很抱歉,我完全误解了你的意图,让我更新我的答案。@Romofa23好的,很抱歉,另一个答案在你的问题中实际上是正确的(我没有阅读所有代码来完全理解你的目标)。我更新了一个解决您问题的示例。谢谢!我要试试看迪耶!成功了!!!非常感谢。