因此,我的應用程序讓用戶在網格上放置塊,如果用戶用相同的套裝或顏色排隊3個或更多的塊,則會發生某些事情。當玩家放置塊I調用這個方法:我該如何阻止「IndexOutOfBoundsException」問題?
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異常。有沒有辦法來防止這個錯誤?
這可能是因爲數組自動排序元素的數量並將數量降低到數組中正確數目的元素,並且我仍然有標記爲高於數組大小的元素。我仍然是新手,所以我的分析可能不正確。請提醒我,如果是這種情況,並幫助我找到解決辦法。 謝謝。
edirted *潤色()函數-------
@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);
}
}
}
剩下的只是很長,只是檢查其他塊彼此相鄰..
首先嚐試添加堆棧跟蹤,第二個最有可能的錯誤將在那長長的一行,如何分裂的東西一點點,但所以你可以得到一個更好的stacktrace?創建一個實例並將其作爲參數傳遞。這個錯誤意味着你正在爲數組/列表等調用一個無效的索引。我打賭在這裏:'新的Vector2(rect_mouse.x,rect_mouse.y)',但我不知道什麼是Vector2。 – porfiriopartida
您可以將堆棧跟蹤添加到問題中。 – porfiriopartida
因爲無知,你分裂的東西是什麼意思? Vector2是塊的線,新塊是類塊的實例,塊ID是塊的類型,塊計數是數組內塊的索引號 – GfxandCode