-1

所以我的应用程序允许用户在网格上放置块,如果用户排列 3 个或更多块具有相同的西装或颜色,那么就会发生一些事情。当玩家放置一个块时,我调用这个方法:

   blocks_.add(new Block(new Vector2(rect_mouse.x, rect_mouse.y), blocks_.get(0).blockID, blockCount)); 

当您将 3 个或更多放在一起时,我称这些方法为:

    blocks_.removeValue(blocks_.get(left_bravo_indexNum), true);
    blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);
    blocks_.removeValue(blocks_.get(right_alpha_indexNum), true);


    stack:
    Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: 13
at com.badlogic.gdx.utils.Array.get(Array.java:125)
at com.jrp.mygearapp.GameScreen.touchUp(GameScreen.java:1443)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:186)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

这是为了删除块,但它导致了这个 IndexOutOfBoundsException。有没有办法防止这个错误?

这可能是因为数组自动对元素数量进行排序并将数量降低到数组中正确的元素数量,并且我仍然有标记为高于数组大小的元素。我还是个新手,所以我的分析可能不正确。如果是这种情况,请提醒我并帮助我找到解决方法。谢谢。

编辑* TouchUp() 函数

   @Override
     public boolean touchUp(int x, int y, int pointer, int button) {

    if (button == 0)  {

        display_blockCheck = false;

        ////set blockCount to the size of blockArray so blocks can properly be indexed              
        blockCount = blocks_.size;

        if (!overlap) {

            Gdx.app.log("Block Added", "x: " + x + " y: " + y);

            updateQueueBlocks();


            //add block
            Vector2 rect_vector = new Vector2(rect_mouse.x, rect_mouse.y);
            Block block = new Block(rect_vector,blocks_.get(0).blockID, blocks_.size);

            blocks_.add(block);             

     if (center_charlie_suit == "Square") {

                center_charlie_bool = true;

                if (right_bravo_suit == "Square") {

                    right_bravo_bool = true;

                    if (right_alpha_suit == "Square") {

                        Gdx.app.log("3-pair", "Square:345:lr");

                        right_alpha_bool = true;

                        //call 3-pair event
                        blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);
                        blocks_.removeValue(blocks_.get(right_alpha_indexNum), true);
                        blocks_.removeValue(blocks_.get(right_bravo_indexNum), true);
                        }
                }
            }

其余的真的很长,只是检查彼此相邻的其他块..

4

1 回答 1

1

你是对的,当你删除块时,索引会改变。

您没有显示 Collectionblocks_是什么类型(Vector2?,您写了吗?),但是,与其跟踪元素的索引,不如简单地跟踪元素本身并调用remove()以查找和删除该元素。

于 2013-10-02T00:47:35.153 回答