Here is the code of bomb:
private int x, y;
private Texture bTexture;
private boolean bomb_explode;
private Timer timer;
private TimerTask task;
private int timeLeft = 0;
private TileMap map;
public static List<Bomb> bombs = new ArrayList<Bomb>();
public Bomb(TileMap map, int x, int y) {
this.map = map;
this.x = x;
this.y = y;
bTexture = new Texture("bomb.png");
bomb_explode = false;
bombs.add(this);
createTimer();
}
public void render(SpriteBatch batch, float delta) {
batch.draw(bTexture, x * Settings.SCALED_TILE_SIZE, y * Settings.SCALED_TILE_SIZE,
Settings.SCALED_TILE_SIZE, Settings.SCALED_TILE_SIZE);
if(timeLeft > 1f) {
bomb_explode = true;
}
if(bomb_explode) {
for(int i = 0; i <= 1; i++) {
if(x + i <= map.getWidth()) {
batch.draw(new Texture("flame.png"),
((x + i) * Settings.SCALED_TILE_SIZE), y * Settings.SCALED_TILE_SIZE,
Settings.SCALED_TILE_SIZE, Settings.SCALED_TILE_SIZE);
}
if(y + i <= map.getHeight()) {
batch.draw(new Texture("flame.png"), x * Settings.SCALED_TILE_SIZE,
((y + i) * Settings.SCALED_TILE_SIZE), Settings.SCALED_TILE_SIZE,
Settings.SCALED_TILE_SIZE);
}
if(x - i >= 0) {
batch.draw(new Texture("flame.png"),
((x - i) * Settings.SCALED_TILE_SIZE), y * Settings.SCALED_TILE_SIZE,
Settings.SCALED_TILE_SIZE, Settings.SCALED_TILE_SIZE);
}
if(y - i >= 0) {
batch.draw(new Texture("flame.png"), x * Settings.SCALED_TILE_SIZE,
((y - i) * Settings.SCALED_TILE_SIZE), Settings.SCALED_TILE_SIZE,
Settings.SCALED_TILE_SIZE);
}
}
}
}
public void Boom() {
bomb_explode = false;
for(int i = 0; i <= 1; i++) {
if(map.getTile(x, y).getPlayer() != null) {
map.getTile(x, y).getPlayer().Die();
}
if(x + i <= map.getWidth() && map.getTile(x+i, y).getPlayer() != null) {
map.getTile(x+i, y).getPlayer().Die();
}
if(x - i >= 0 && map.getTile(x-i, y).getPlayer() != null) {
map.getTile(x-i, y).getPlayer().Die();
}
if(y + i <= map.getHeight() && map.getTile(x, y+i).getPlayer() != null) {
map.getTile(x, y+i).getPlayer().Die();
}
if(y - i >= 0 && map.getTile(x, y-i).getPlayer() != null) {
map.getTile(x, y-i).getPlayer().Die();
}
}
bombs.remove(this);
}
public void createTimer() {
timeLeft = 0;
task = new TimerTask() {
@Override
public void run() {
++timeLeft;
if(timeLeft > 3) {
bomb_explode = true;
Boom();
clearTimer();
}
}
};
timer = new Timer();
timer.schedule(task, 1, 500);
}
public void clearTimer() {
timer.cancel();
timer.purge();
timer = null;
}
In the screen class, I'm rendering the bomb like this :
for(Bomb b : Bomb.bombs) {
b.render(batch, delta);
}
Here is the error I'm getting:
Exception in thread "LWJGL Application" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at bomberman.screens.GameScreen.render(GameScreen.java:41)
at com.badlogic.gdx.Game.render(Game.java:46)
at bomberman.game.GameObject.render(GameObject.java:23)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
How can I fix it?
GameScreen.render()code as well? It seems this is where the problem is located. \$\endgroup\$