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.

2
  • 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? Commented Apr 6, 2014 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 Commented Apr 6, 2014 at 20:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Ive tried this the error is mainly coming from the for loop that updates/removes the battleObjects.
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.
the monster update is calling the createNewBattle method which is changing the game state and add the monster and player to the battle objects.
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: stackoverflow.com/questions/993025/…
"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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.