我想用andengine library
做一個遊戲。如何修復此錯誤? 「java.lang.IndexOutOfBoundsException」
當雪碧Enemy1Sprite
到達相機的頂部,我取下它,這將引發異常:
java.lang.IndexOutOfBoundsException Invalid Index 12 size is 12
我必須取下enemy1Sprite
,因爲它不斷創造子彈的精靈從相機中。
這是代碼。
類enemy1:
package es.uah.juegomentos;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
public class Enemy1 extends Sprite {
boolean abajo = true;
public Enemy1(TextureRegion pTextureRegion) {
super(0, 0, pTextureRegion);
this.setPosition(JuegoMentosActivity.RANDOM.nextInt(JuegoMentosActivity.CAMERA_WIDTH), -10);
TimerHandler Enemy1fire = new TimerHandler(0.75f, true, enemigo1fireCallback);
JuegoMentosActivity.getmGameScene().registerUpdateHandler(Enemy1fire);
}
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
float y = getY();
if (y >= 275) {abajo = false;}
if (abajo) {y = y + pSecondsElapsed * 125.0f;}
else {y = y - pSecondsElapsed * 125.0f;}
this.setPosition(getX(), y);
if (getY()<-10){this.getParent().detachChild(this);}
}
ITimerCallback enemigo1fireCallback = new ITimerCallback(){
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
bala1 mbala1;
mbala1 = new bala1(getX()+(64*1/2),getY()+64,JuegoMentosActivity.getMbala1Texture().getTextureRegion(),true);
JuegoMentosActivity.getmGameScene().attachChild(mbala1);
}
};
}
在場景中創建新的敵人:
//Creamos el sprite del enemigo uno
ITimerCallback enemigo1CreatorCallback = new ITimerCallback(){
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
mEnemy1Sprite = new Enemy1(mEnemy1Texture.getTextureRegion());
mGameScene.attachChild(mEnemy1Sprite);
}
};
TimerHandler Enemy1Creator = new TimerHandler(3.0f, true, enemigo1CreatorCallback);
mGameScene.registerUpdateHandler(Enemy1Creator);
感謝
您是否考慮過發佈實際拋出異常的代碼部分?當你在這個時候,* complete *異常堆棧? – Marcelo 2012-03-22 14:30:33
順便說一下,快速谷歌搜索會告訴你,大小爲12的數組的最後一個索引實際上是11,因爲數組索引是基於0的Java。 – Marcelo 2012-03-22 14:31:43