removing enemy and bullet in collision java -


i making simple 2d game in java. have created controller class , created linked lists zombies(enemies) , bullets. in gamestate class trying use each loops loop through linked lists , detect if there collision bullet , zombie. way laid out below in code example, enemy can removed game, if try remove bullet well, game crashes. wondering if ideas on how can remove both entities after collision occurs.

this error when bullet collides enemy

exception in thread "thread-1" java.util.concurrentmodificationexception @ java.util.linkedlist$listitr.checkforcomodification(unknown source) @ java.util.linkedlist$listitr.next(unknown source) @ android.game.gamestate.update(gamestate.java:71) @ android.game.game.update(game.java:121) @ android.game.game.run(game.java:101) @ 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 = 0; i<b.size(); i++){         tempbullet = b.get(i);          tempbullet.tick();     }      for(int = 0; i<z.size(); i++){         tempzombie = z.get(i);          tempzombie.update();     }   }  public void draw(graphics g){     for(int = 0; < b.size(); i++){         tempbullet = b.get(i);          tempbullet.draw(g);     }      for(int = 0; < 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); } } 

use iterator go through list, , use remove() method remove current element collection:

for(iterator<zombie> = c.z.iterator(); it.hasnext(); ) {     zombie zombie = it.next();     if (collisionwithbullet)         it.remove(); } 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -