0

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();
        }
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45
  • I still get the same error, why don't you just answer my question since you know a lot! – Kevin Bryan Jun 01 '15 at 11:57
  • Your issue is nothing to do with Pools, Sprites etc and everything to do with concurrent modification. John Skeet is absolutely correct to mark it as a duplicate. I suggest you read and fully digest the question he linked. – Phil Anderson Jun 01 '15 at 12:54

0 Answers0