让我试着解释一下现在发生了什么:当我将 3 个“X”并排放置时,我只是想删除它们,但它只在部分时间发生,有时它会删除所有这些,而其他时候它只删除 1 或只有2个。这些块是放置在数组列表中的 Block 类的实例......此外,为了更广泛地理解:我将随机将块放置在整个网格中并尝试填充它,有时就像我将块添加到网格中的其他块开始消失。此外,当我填满大部分网格时,我将继续并开始删除它们,但是当我开始将鼠标悬停在块上并右键单击它们以删除它们时,其他块将被删除,而不是我悬停在上面的块。如您所见,blockCount 是将鼠标悬停在块上时返回的数字,换句话说,它是数组列表中对象的索引号(尽管有时看起来不是这样)。数组列表发生了什么?
这是发生了什么的stackTrace:显然与尝试删除不再存在的索引有关吗?我能做些什么?我是不是错了?这个堆栈跟踪是我的大问题,无论我是单独删除块还是当我尝试连续匹配 3 个块时,我得到的索引大于大小错误.. 请帮助迷失的灵魂....
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 = "";
}
}