0
public Game() {
        gameState = OVERWORLD;

        objects = new ArrayList<GameObject>();
        remove = new ArrayList<GameObject>();

        battleObjects = new ArrayList<GameObject>();
        battleRemove = new ArrayList<GameObject>();

        player = new Player(Display.getWidth() / 2 - player.SIZE / 2, Display.getHeight() / 2 - player.SIZE / 2);

        objects.add(player);
        objects.add(new Circle(32, 32, player));
        objects.add(new Imp(300, 100, player));
    }


public void update() {
            if (gameState == BATTLE) {
                for (GameObject i : battleObjects) {
                    if (!i.isRemoved())
                        i.update();
                    else
                        battleRemove.add(i);
                }
            }
            else {
                for (GameObject i : objects) {
                    if (!i.isRemoved())
                        i.update();
                    else
                        remove.add(i);
                }
            }
        }

        public void render() {
            if (gameState == BATTLE) {
                for (GameObject i : battleObjects)
                        i.render();
            }
            else {
                for (GameObject i : objects)
                    i.render();
            }
        }

        public static void createNewBattle(Player p, Monster m) {
            battleObjects.add(p);
            battleObjects.add(m);

            gameState = BATTLE;
        }

I completely understand why its happening but I cannot for the life of me find a way to fix the problem... Can anyone help me??? I sat here for hours trying to find another way of doing this. I was think of just creating an array with the battle objects.

Greymouth
  • 15
  • 4
  • I am curious about the GameObject class (especially this "isRemoved" method). Why you need to (re)move an object to a separate collection on top of that? – Andreas Tasoulas Apr 06 '14 at 20:02
  • In my game there is an item and it can be picked up, which thereby sets a flag to remove the object from the world and place it in the player inventory. And its like a pokemon rpg with random battles so I created a separate battle arraylist for rendering and updating only the objects in the battle – Greymouth Apr 06 '14 at 20:05

1 Answers1

0

The only safe way to modify a collection is using the remove method from an iterator, so use an iterator instead of a for each and you will be just fine: Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

Community
  • 1
  • 1
eduardohl
  • 1,176
  • 2
  • 10
  • 23
  • Ive tried this the error is mainly coming from the for loop that updates/removes the battleObjects. – Greymouth Apr 06 '14 at 19:46
  • 1
    I guess you update method might be trying to do something that changes the objects list. Inside a for each, you cannot modify the objects collection. – eduardohl Apr 06 '14 at 19:55
  • the monster update is calling the createNewBattle method which is changing the game state and add the monster and player to the battle objects. – Greymouth Apr 06 '14 at 20:00
  • Right, and that's what you can't do using the for each... you cannot add new elements into objects or remove elements from it. You can create a secondary lists with the objects you want to add AFTER the for each. Check it here: http://stackoverflow.com/questions/993025/java-adding-elements-to-a-collection-during-iteration – eduardohl Apr 06 '14 at 20:04
  • "How about building a Queue with the elements you want to iterate over; when you want to add elements, enqueue them at the end of the queue, and keep removing elements until the queue is empty. This is how a breadth-first search usually works." Im not quite sure what he's saying here. – Greymouth Apr 06 '14 at 20:07
  • Really, you can't modify you list while using for each: http://stackoverflow.com/questions/11177348/how-to-add-element-in-list-while-iterating-in-java – eduardohl Apr 06 '14 at 20:10
  • Figured it out. The player was constantly colliding with the monster therefore constantly iterating the battleObjects over itself. – Greymouth Apr 06 '14 at 20:23