Original Post
I'm making a java game at the moment and when I happen to kill two enemies at the exactly same time then the game crashes. I suppose it's something with threads and serializable or something. But I don't know. I use the following code when I'm removing objects from the arrayList. The problem appears when I kill two enemies at EXACTLY the same time. Then it freezes and say:
Exception in thread "Thread-3" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at Main.checkCollisions(Main.java:168)
at Main.run(Main.java:400)
at java.lang.Thread.run(Unknown Source)
// Skott med fiende
Iterator b = bubblor.iterator();
while(b.hasNext()){
Iterator e = enemies.iterator();
Bullet bob = b.next();
while(e.hasNext()){
Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove();
e.remove();
} } }
Exception in thread "Thread-3" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at Main.checkCollisions(Main.java:168)
at Main.run(Main.java:400)
at java.lang.Thread.run(Unknown Source)
// Skott med fiende
Iterator
while(b.hasNext()){
Iterator
Bullet bob = b.next();
while(e.hasNext()){
Enemy fof = e.next();
Rectangle r1 = fof.getBounds();
Rectangle r2 = bob.getBounds();
if(r1.intersects(r2) ){
b.remove();
e.remove();
} } }