I'm trying to remove my enemy sprite whenever they collide to a rectangle,I'm also using Pools.
while(enemyIterator.hasNext()){
Sprite sprite=enemyIterator.next(); //this is the line 65
if(enemyRect.overlaps(treeObj.treeRect))
removeEnemy(sprite);
}
then on remove enemy method:
public void removeEnemy(Sprite sprite){
enemies.remove(sprite);
Pools.free(sprite);
}
I get this error:
Exception in thread "LWJGL Application"java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.dreamroad.savethetree.EnemyClass.update(EnemyClass.java:65)
at com.dreamroad.savethetree.MyGdxGame.render(MyGdxGame.java:51)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
I followed the instruction from the question he linked and I still get the error, for now I'm only trying to remove the rectangles.
for(Iterator<Rectangle> rectIterator=rectangles.iterator();rectIterator.hasNext();){
Rectangle nextRect=rectIterator.next();
if(nextRect.overlaps(treeObj.treeRect))
rectIterator.remove();
}