It doesn’t work out too well.
Every time I remove an object from my vector, this happens:
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.javadaemon.GameEngine.EntityManager.update(EntityManager.java:42)
at com.javadaemon.GameEngine.GameEngine.update(GameEngine.java:59)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:657)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
at com.javadaemon.GameEngine.GameEngine.main(GameEngine.java:75)
Sun Feb 13 16:20:07 CET 2011 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
at com.javadaemon.GameEngine.GameEngine.main(GameEngine.java:75)
BUILD SUCCESSFUL (total time: 8 seconds)
I thought you could remove entries in vectors?
This is my code:
public Vector<Entity> entities;
public EntityManager() {
entities = new Vector();
}
public void update(int delta) {
for (Entity e : entities) {
e.update(delta);
if (e instanceof Rocket) {
Rocket rocket = (Rocket) e;
if (rocket.isExploded) {
entities.remove(e);
}
}
}
How do I handle vectors correctly, because in the javadocs it sais that vectors can shrink and grow as needed?
EDIT: It seems that when I tweak the update amount on the rockets the problem disappears.
This is the rocket update():
@Override
public void update(int delta) {
if (timePassed + delta > updateTime) {
y -= 1;
timePassed = 0;
if (y < World.MINIMUM_EXPLODE_HEIGHT) {
isExploded = true;
}
} else {
timePassed += delta;
}
}
The Y int of the rocket starts at around 800, and MINIMUM_EXPLODE_HEIGHT is around 100.
This is how entities are updated:
public void addRocket() {
int x = generator.nextInt(GameEngine.width);
Rocket r = new Rocket(x, 5, 20);
entities.add(r);
}
public void update(int delta) {
timePassed += delta;
if (timePassed > addRocketTime) {
timePassed = 0;
addRocket();
}
for (Entity e : entities) {
e.update(delta);
/*if (e instanceof Rocket) {
Rocket rocket = (Rocket) e;
if (rocket.isExploded) {
entities.remove(e);
}
}*/
}
for (Entity e : entities) {
if (e instanceof Rocket) {
Rocket rocket = (Rocket) e;
if (rocket.isExploded) {
entities.remove(e);
}
}
}
}