将新对象添加到列表中

时间:2015-03-12 17:32:18

标签: libgdx concurrentmodification

我有一个游戏,玩家跳过砖块然后那些砖块消失。当我的玩家死亡时,我想再次创建这些块。 这是我的块类

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);
         }
    }

主要是当我的角色死了,我检查(当我触摸块时,我减少了我的折扣WeekBrick.discount--;

if(WeekBrick.count != WeekBrick.discount){
        addWeekBricks();
 }

在这里我添加了新的砖

private void addWeekBricks(){
    for(int i = 0; i < (2 * WeekBrick.count); i++){
        list.add(new WeekBrick(WeekBrick.getItemFromList(i), WeekBrick.getItemFromList(++i), true));
    }
}

当我将我的对象添加到列表时,我收到了此错误

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)

为什么我收到此错误?我做错了什么? 的修改: 当我做这个

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));
            }
        }

在我的渲染方法中,当我按 M 所有工作!那怎么可能呢?当我在&#39;游戏并按 M 所有砖更新,一切正常,但当我输了,我得到了那个例外......

0 个答案:

没有答案