我如何停止此"IndexOutOfBoundsException";问题? [英] how do i stop this "IndexOutOfBoundsException" issue?

查看:160
本文介绍了我如何停止此"IndexOutOfBoundsException";问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我该应用程序允许用户在网格上放置块,如果用户将3个或更多具有相同颜色或颜色的块排列在一起,则会发生某些情况.当玩家放置一个方块时,我调用此方法:

So I the app lets a user place down blocks on a grid, if the user lines up 3 or more blocks with the same suit, or color, then something happens. When player places a block I call this method:

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

当您将3个或更多放置在一起时,我将调用以下方法:

When you place 3 or more together I call these methods:

    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.有办法防止此错误吗?

This was intended to remove the blocks, but it resulted in this IndexOutOfBoundsException. Is there a way to prevent this error?

之所以会发生这种情况,是因为数组会自动对元素数量进行排序,并将数量减少到数组中元素的正确数量,而我仍然有一些元素被标记为高于数组的大小.我仍然是新手,因此我的分析可能不正确.如果是这种情况,请提醒我,并帮助我找到解决方法. 谢谢.

This could be occurring because the array auto sorts the number of elements and lowers the number to the correct number of elements in the array, and I still have elements that are labeled higher then the size of the array. I am still a novice, so my analysis could be incorrect. Please alert me if this is the case and help me find a fix. Thanks.

编辑 * TouchUp()函数-------

edirted* TouchUp() function-------

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

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

the rest is just really long and just checks for other blocks next to each other..

推荐答案

您是对的,在删除块时,索引会更改.

You're right, as you remove the blocks, the indexes change.

您没有显示Collection blocks_的类型(Vector2 ?,您写的是吗?),但是,不是跟踪元素的索引,而是跟踪元素本身并调用remove()来查找并删除该元素.

You don't show what type of Collection blocks_ is (Vector2?, did you write it?), however, rather than tracking the indices of the elements, simply track the elements themselves and call remove() to find and remove that element.

这篇关于我如何停止此"IndexOutOfBoundsException";问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆