我使用OpenGL ES为Android编写了2D游戏,我遇到了问题。我一直在使用eclipse。当我在模拟器上开始游戏时,我只看到背景图像,我听到背景音乐和声音效果。屏幕上没有图形和精灵。在真实设备上(使用Android 2.2)是相同的结果。这是我的LogCat:
E / AndroidRuntime(328):致命异常:GLThread 9
E / AndroidRuntime(328):java.lang.IndexOutOfBoundsException:无效 索引1,大小为0 E / AndroidRuntime(328):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) E / AndroidRuntime(328):at java.util.ArrayList.get(ArrayList.java:311)
E / AndroidRuntime(328):at arek.jumper.GameScreen.updateGameOver(GameScreen.java:166)
E / AndroidRuntime(328):at arek.jumper.GameScreen.update(GameScreen.java:86)
E / AndroidRuntime(328):at game.framework.impl.GLGame.onDrawFrame(GLGame.java:94) E / AndroidRuntime(328):at android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1332)
E / AndroidRuntime(328):at android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1116)
updateGameOver方法:
private void updateGameOver() {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type != TouchEvent.TOUCH_UP)
continue;
game.setScreen(new MainMenuScreen(game));
}
}
更新方法:
@覆盖
public void update(float deltaTime) {
if(deltaTime > 0.1f)
deltaTime = 0.1f;
switch(state) {
case GAME_READY:
updateReady();
break;
case GAME_RUNNING:
updateRunning(deltaTime);
break;
case GAME_PAUSED:
updatePaused();
break;
case GAME_LEVEL_END:
updateLevelEnd();
break;
case GAME_OVER:
updateGameOver();
break;
}
}
和onDrawFrame:
@覆盖
public void onDrawFrame(GL10 gl) {
GLGameState state = null;
synchronized(stateChanged) {
state = this.state;
}
if(state == GLGameState.Running) {
float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
startTime = System.nanoTime();
screen.update(deltaTime);
screen.present(deltaTime);
}
if(state == GLGameState.Paused) {
screen.pause();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}
if(state == GLGameState.Finished) {
screen.pause();
screen.dispose();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}
}
onCreate方法:
@覆盖 public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
glGraphics = new GLGraphics(glView);
fileIO = new AndroidFileIO(getAssets());
audio = new AndroidAudio(this);
input = new AndroidInput(this, glView, 1, 1);
PowerManager powerManager = (PowerManager)
getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}
这是OpenGL ES库的一些问题吗? 有人对此有一些建议吗? 怎么解决这个? 非常感谢
答案 0 :(得分:1)
更改
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type != TouchEvent.TOUCH_UP)
continue;
game.setScreen(new MainMenuScreen(game));
}
到
for (TouchEvent touchEvent : touchEvents){
if(touchEvent.type != TouchEvent.TOUCH_UP)
continue;
game.setScreen(new MainMenuScreen(game));
}
这样您就不会有风险要求列表中的非现有对象。如果列表为空,它就不会运行。
我不知道它是否能完全解决你的问题;但是你要摆脱indexOutOfBOundsException。