移除碰撞中的敌人和子弹 java

removing enemy and bullet in collision java

我目前正在 java 制作一个简单的 2d 游戏。我创建了一个控制器 class 并为僵尸(敌人)和子弹创建了链表。然后在我的 gameState class 中,我尝试使用 for each 循环遍历链表并检测是否与子弹和僵尸发生碰撞。然而,在我的代码示例中,只有敌人可以从游戏中移除,如果我也尝试移除子弹,游戏就会崩溃。我想知道我是否可以得到一些关于如何在发生碰撞后删除两个实体的想法。

这是子弹与敌人相撞时出现的错误

Exception in thread "Thread-1" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at android.game.GameState.update(GameState.java:71)
at android.game.Game.update(Game.java:121)
at android.game.Game.run(Game.java:101)
at java.lang.Thread.run(Unknown Source)

在 GameState 中 class:

for(Zombie zombie : c.z)
    {
        if(player.getX() > zombie.getX()){
            zombie.setX(zombie.getX() + 1);
        }

        if(player.getX() < zombie.getX()){
            zombie.setX(zombie.getX() - 1);
        }

        if(player.getY() > zombie.getY()){
            zombie.setY(zombie.getY() + 1);
        }

        if(player.getY() < zombie.getY()){
            zombie.setY(zombie.getY() - 1);
        }

        if(player.getBounds().intersects(zombie.getBounds())){

            kills ++;
            c.removeZombie(zombie);
            System.out.println("kills" + kills);

        }

        for(Bullet bullet : c.b){
            if(zombie.getBounds().intersects(bullet.getBounds())){
                //c.removeBullet(bullet);
                c.removeZombie(zombie);
                kills++;

                System.out.println("kills" + kills);        
            }

        }

    }

控制器class:

public class Controller {

LinkedList<Bullet> b = new LinkedList<Bullet>();
LinkedList<Zombie> z = new LinkedList<Zombie>();

Bullet TempBullet;
Zombie TempZombie;

public Controller(){
    addZombie(new Zombie (200,600));
    addZombie(new Zombie (400,650));
}

public void tick(){
    for(int i = 0; i<b.size(); i++){
        TempBullet = b.get(i);

        TempBullet.tick();
    }

    for(int i = 0; i<z.size(); i++){
        TempZombie = z.get(i);

        TempZombie.update();
    }


}

public void draw(Graphics g){
    for(int i = 0; i < b.size(); i++){
        TempBullet = b.get(i);

        TempBullet.draw(g);
    }

    for(int i = 0; i < z.size(); i++){
        TempZombie = z.get(i);

        TempZombie.render(g);
    }

}

public void addBullet(Bullet block){
    b.add(block);
}

public void removeBullet(Bullet block){
    b.remove(block);
}

public void addZombie(Zombie block){
    z.add(block);
}

public void removeZombie(Zombie block){
    z.remove(block);
}
}

您不能使用 for each 循环来检查与子弹的碰撞,然后同时移除同一颗子弹。相反,您可以尝试标记要删除的项目符号,然后在每个循环的碰撞检测完成后删除它们。

如果您在执行 for-each 循环时删除元素,则会发生 ConcurrentModificationException。解决此问题的一种方法是将循环更改为索引循环:

for (int i = c.z.size() - 1; i >= 0; i--) {
    Zombie zombie = c.z.get(I);

那么删除僵尸应该是安全的,而不必将要删除的僵尸存储在单独的集合中。

使用 Iterator to go through your list, and use the remove() 方法从 Collection 中删除当前元素:

for(Iterator<Zombie> it = c.z.iterator(); it.hasNext(); ) {
    Zombie zombie = it.next();
    if (collisionWithBullet)
        it.remove();
}