I have a game where player jump over bricks and then those bricks disappear. When my player died I want to create those blocks again. Here are my block class
private Rectangle full, top;
private Sprite sprite;
private static ArrayList<Integer> list = new ArrayList<Integer>(); // list of block coordinates
private int x, y; // block coordinates
public static int count, discount; // count - all blocks // discount - removed blocks
public WeekBrick(int x, int y){
this.x = x;
this.y = y;
list.add(x);
list.add(y);
count++;
discount = count;
full = new Rectangle(0, 0, 64, 64);
top = new Rectangle(0, 46, 64, 20);
sprite = new Sprite(TextureManager.weekBrick);
sprite.setSize(64, 64);
setPosition(x, y);
}
}
And here in main when my character died I check (when I touch the block I decrease my discount like that WeekBrick.discount--;
)
if(WeekBrick.count != WeekBrick.discount){
addWeekBricks();
}
And here I add new Brick
private void addWeekBricks(){
for(int i = 0; i < (2 * WeekBrick.count); i++){
list.add(new WeekBrick(WeekBrick.getItemFromList(i), WeekBrick.getItemFromList(++i), true));
}
}
And when I add my object to list I got this error
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 com.mygdx.game.MyGame.mainGame(MyGame.java:484)
at com.mygdx.game.MyGame.render(MyGame.java:189)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Why I get this error? What I'm doing wrong? EDIT: When I make this
if(Gdx.input.isKeyJustPressed(Input.Keys.M)){
for(int i = 0; i < (2 * WeekBrick.count); i++){
list.add(new WeekBrick(WeekBrick.getItemFromList(i), WeekBrick.getItemFromList(++i), true));
}
}
in my render method and when I press M all work! So how is it possible? When I 'in' the game and press M all bricks update and all works fine, but when I lose I get that exception...