我该如何修复此阵列问题?

时间:2013-10-04 06:35:17

标签: java arrays libgdx

lemme尝试解释现在发生的事情:当我将3个“X”放在彼此旁边时我只是想删除它们,但它只发生了部分时间,有时它会删除所有这些,有时它会删除它们只有1或只有2。这些块是Block类放在arraylist中的实例.... 此外,为了更广泛的理解:我将随机地将块放在网格上并尝试填充它,有时当我将块添加到网格中时,其他块开始消失。此外,当我填满大部分网格时,我会继续并开始删除它们,但是当我开始盘旋在块上并右键单击它们以删除它们时,其他块将被删除而不是悬停在其上的块。正如您所看到的,blockCount是将鼠标悬停在块上时返回的数字,换句话说就是它在arraylist中的对象的索引号(尽管有时它似乎不是这样)。什么是arraylist?

下面是关于发生什么事情的堆栈追踪:显然与尝试删除不再存在的索引有关吗?我能做什么?我错了吗?这个堆栈跟踪是我的大问题我单独删除块或当我尝试连续匹配3时,我得到这个索引更大然后大小错误.. plz帮助一个迷失的灵魂....

     Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 21, Size: 14
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.remove(Unknown Source)
at com.jrp.mygearapp.GameScreen.touchUp(GameScreen.java:1176)
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)

这是TouchUp()方法:

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

  if (button == 0)  {

     display_blockCheck = false;

     if (!overlap) {

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

        updateQueueBlocks();

        //add block

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

        for ( Block b : getBlocks_L()) {

           //check alpha/bravo cross intersections
           if (Intersector.overlapRectangles(b.getBounds(), left_bounds_alpha)) {

              //set indexNum, color, and suit to temporary variables for text info   

              //get block indexNum
              left_alpha_indexNum = b.indexNum;
              //get block color
              left_alpha_color =   b.color_str;
              //get block suit
              left_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), left_bounds_bravo)) {

              //get block indexNum
              left_bravo_indexNum = b.indexNum;
              //get block color
              left_bravo_color =   b.color_str;
              //get block suit
              left_bravo_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), right_bounds_alpha)) {

              //get block indexNum
              right_alpha_indexNum = b.indexNum;
              //get block color
              right_alpha_color =   b.color_str;
              //get block suit
              right_alpha_suit = b.suit_str;



           }

           if (Intersector.overlapRectangles(b.getBounds(), right_bounds_bravo)) {

              //get block indexNum
              right_bravo_indexNum = b.indexNum;
              //get block color
              right_bravo_color =   b.color_str;
              //get block suit
              right_bravo_suit = b.suit_str;



           }

           //center bounding box for cross checker
              if (Intersector.overlapRectangles(rect_mouse, b.getBounds())) {

                 //get block indexNum
                 center_charlie_indexNum = b.indexNum;
                 //get block color
                 center_charlie_color =   b.color_str;
                 //get block suit
                 center_charlie_suit = b.suit_str;

              }


           if (Intersector.overlapRectangles(b.getBounds(), top_bounds_alpha)) {

              //get block indexNum
              top_alpha_indexNum = b.indexNum;
              //get block color
              top_alpha_color =   b.color_str;
              //get block suit
              top_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), top_bounds_bravo)) {

              //get block indexNum
              top_bravo_indexNum = b.indexNum;
              //get block color
              top_bravo_color =   b.color_str;
              //get block suit
              top_bravo_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), bottom_bounds_alpha)) {

              //get block indexNum
              bottom_alpha_indexNum = b.indexNum;
              //get block color
              bottom_alpha_color =   b.color_str;
              //get block suit
              bottom_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), bottom_bounds_bravo)) {

              //get block indexNum
              bottom_bravo_indexNum = b.indexNum;
              //get block color
              bottom_bravo_color =   b.color_str;
              //get block suit
              bottom_bravo_suit = b.suit_str;



           }
 //X*******************************************************************************************
 if (left_alpha_suit == "X" && left_bravo_suit == "X" && center_charlie_suit == "X") {

           Gdx.app.log("3-pair", "X:123:lr");

           blocks_L.remove(left_alpha_indexNum);         
           blocks_L.remove(left_bravo_indexNum);      
           blocks_L.remove(center_charlie_indexNum -2);

           //blocks_.removeValue(blocks_.get(left_alpha_indexNum), true);            
           //blocks_.removeValue(blocks_.get(left_bravo_indexNum), true);         
           //blocks_.removeValue(blocks_.get(blockCount), true);            
            //blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);   

        }

 if (left_bravo_suit == "X" && center_charlie_suit == "X" && right_bravo_suit == "X") {

           Gdx.app.log("3-pair", "X:234:lr");

           blocks_L.remove(left_bravo_indexNum);      
           blocks_L.remove(right_bravo_indexNum);
           blocks_L.remove(center_charlie_indexNum -2);

           /*blocksRemove_.add(blocks_.get(left_bravo_indexNum));
           blocksRemove_.add(blocks_.get(center_charlie_indexNum));
           blocksRemove_.add(blocks_.get(right_bravo_indexNum));

           blocks_.removeAll(blocksRemove_, true);*/
        }

 if (center_charlie_suit == "X" && right_bravo_suit == "X" && right_alpha_suit == "X") {

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

           blocks_L.remove(right_alpha_indexNum);
           blocks_L.remove(right_bravo_indexNum);
           blocks_L.remove(center_charlie_indexNum -2);      

           /*blocksRemove_.add(blocks_.get(center_charlie_indexNum));   
           blocksRemove_.add(blocks_.get(right_alpha_indexNum));
           blocksRemove_.add(blocks_.get(right_bravo_indexNum));   
           blocks_.removeAll(blocksRemove_, true);*/

        }
 }

 button01bool = false;

   }

 if (button == 1) {

     if (overlap) {

        //remove block      
        //blocks_.removeValue(blocks_.get(blockCount), true);      

        blocks_L.remove(blockCount);

     }

     button02bool = false;

  }

  return false;
}

这是mouseMoved()方法:

   @Override
   public boolean mouseMoved(int x, int y) {

  camera.unproject(tmp1.set(Gdx.input.getX(), Gdx.input.getY(), 0));

  mouseVec.x = tmp1.x ;
  mouseVec.y = tmp1.y ;

  rect_mouse.x = (int)mouseVec.x;
  rect_mouse.y = (int)mouseVec.y;


  for (Block b : getBlocks_L()) {


     if (Intersector.overlapRectangles(rect_mouse, b.getBounds())) {

        //mouse and block overlap is true                  
        overlap = true;

        //get block index
        blockCount = b.indexNum;

        tmpColorStr = b.color_str;
        tmpSuitStr = b.suit_str;

        break;

     } else {

        overlap = false;

        left_alpha_indexNum = 0;   
        left_alpha_color =   "";
        left_alpha_suit = "";

        left_bravo_indexNum = 0;   
        left_bravo_color =   "";
        left_bravo_suit = "";

        right_alpha_indexNum = 0;   
        right_alpha_color =   "";
        right_alpha_suit = "";

        right_bravo_indexNum = 0;
        right_bravo_color =   "";
        right_bravo_suit = "";

        center_charlie_color =   "";
        center_charlie_suit = "";
        center_charlie_indexNum = 0;

        top_alpha_indexNum = 0;   
        top_alpha_color =   "";
        top_alpha_suit = "";

        top_bravo_indexNum = 0;   
        top_bravo_color =   "";
        top_bravo_suit = "";

        bottom_alpha_indexNum = 0;   
        bottom_alpha_color =   "";
        bottom_alpha_suit = "";

        bottom_bravo_indexNum = 0;
        bottom_bravo_color =   "";
        bottom_bravo_suit = "";

     }




  }

1 个答案:

答案 0 :(得分:0)

你应该考虑使用libgdx本身提供的Array。它基本上可以做到,ArrayList能够做到,但更快,更有效。

也许你可以解释一下代码应该做什么?只需阅读您对问题的描述,就很难理解您要做的事情。

Array也可以通过传递索引并使用方法

来删除对象
.removeIndex(int index);

也许这会对你有帮助。

编辑:

您应该将所有内容编程为一种方式,不会发生任何数组超出范围的异常。尝试这样的程序结构:

块类的外观示例:

public class Block {
   private int x,y;
   public Block(int x, int y) {
      this.x = x;
      this.y = y;
   }
   public boolean check(int x, int y) {
      if(this.x == x && this.y == y) {
         return true;
      } else {
         return false;
      }
   }
}

然后当鼠标点击[leftclick]时出现:

int x = ...;
int y = ...;
calculate here x and y values of the clicked block by using a formular ( don't use this slow overlay-stuff, when you're in a gridworld);
boolean blockAlreadyExists = false;
for(Block block: yourBlockArray) {
   blockAlreadyExists = block.check(x, y);
   if(blockAlreadyExists) {
      break; // you don't need to check the remaining blocks, aslong you know that the block with the given x and y values already exists
   }
}
if(!blockAlreadyExists) {
   yourBlockArray.add(new Block(x, y); // add the new block, when it's not already there
}

然后当鼠标点击[右键单击]时出现:

int x = ...;
int y = ...;
calculate your x and y values the same way as above;
Block blockToDelete;
for(Block block: yourBlockArray) {
   if(block.check(x, y)) {
      blockToDelete = block;
      break; // you don't need to check the remaining blocks, aslong you deleted the clicked block and there cannot exist another one with the same x and y values.
   }
}
if(blockToDelete != null) {
   yourBlockArray.remove(blockToDelete, true);
}

也许这可以帮助你:)

相关问题