0

I'm creating a game and it's pretty much a top down shooter kind of like space invaders.

Well, I am currently handling the removal of entities from a linked list in an enhanced for loop and as we all know we cant just add stuff to a list and take away stuff from a list at the same time.

I keep getting the following error:

conccurentModificationException

My question is how can I efficiently handle the addition and removal of entities from a Linked-list without getting an error.

Here is my World Class

package game;

import java.awt.Graphics;
import java.util.LinkedList;

import data.Guns;
import entity.Beast;
import entity.Entity;
import entity.Player;
import entity.Projectile;
import external.ImageLoader;
import handlers.Handler;

public class World {

    private Player player;

    private Handler handler;

    // List of all Game Entities

    public static LinkedList<Entity> gameFools = new LinkedList<Entity>();

    // Constructor for World , this is created once the Game State is instantiated

    public World(Handler handler) {
        this.handler = handler;

        // The player is the first entity added to the list of course

        player = new Player(handler, null, Guns.tier1, 64, 64, 400, 900, 500, 5, 0, 0);
        gameFools.add(player);

    }

    public void update() {

        // Randomly spawns in a new beast npc

        if ((int) (Math.random() * 100) == 0) {
            gameFools.add(new Beast(handler,
                    ImageLoader.loadImage("C:\\Users\\Michael\\workspace\\TopDown\\res\\badguys\\mig1.png"), 32, 32,
                    (int) (Math.random() * 700 + 50), 50, 10, 1, -1, -1, false, false, false, false, false));
        }

        //This is the for loop that is used to remove entitys from the linked list

        for (Entity e : gameFools) {

            // If the object is not within the screen, it gets removed

            if (e.getY() <= 0) {
                gameFools.remove(e);
            }

            // Once the Projectiles Range is up, it is removed

            if (e.isProjectile()) {

                if (((Projectile) e).getRange() <= 0 ) {
                    gameFools.remove(e);
                }

            }

            e.update();
        }
        System.out.println("Current Objects entities in game " + gameFools.size());

    }

    // Getters and Setters

    public void render(Graphics g) {
        for (Entity e : gameFools) {
            e.render(g);
        }
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

}
coatless
  • 20,011
  • 13
  • 69
  • 84
  • Getting `The URL you requested has been blocked` on clicking URL – Saravana Jul 05 '16 at 04:16
  • Searching before posting a question will often give you the answer much quicker than writing up a new question and waiting for someone to do the search for you, – Andreas Jul 05 '16 at 04:35

0 Answers0