Search code examples
javacollision

removing enemy and bullet in collision java


I am currently making a simple 2d game in java. I have created A controller class and created linked lists for zombies(enemies) and bullets. Then in my gameState class I am trying to use for each loops to loop through the linked lists and detect if there is a collision with the bullet and the zombie. However the way it is laid out below in my code example, only the enemy can be removed from the game, if I try to remove the bullet as well, the game crashes. I was wondering if I could get some ideas on how i can remove both entities after a collision occurs.

this is the error I get when the bullet collides with the enemy

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)

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

        }

    }

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

Solution

  • Use an Iterator to go through your list, and use the remove() method to remove the current element from your Collection:

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