Question

I wrote the 2d game for Android using OpenGL ES and I have problem. I've been using eclipse. When I start the game on the emulator I see only background image and I hear background music and sound effects. There are no graphics and sprites on the screen. On the real device (with Android 2.2) is the same result. This is my LogCat:

E/AndroidRuntime(328): FATAL EXCEPTION: GLThread 9

E/AndroidRuntime(328): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 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)

The updateGameOver method:

 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));
        }
        }

The update method:

@Override

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;
}
}

and onDrawFrame:

@Override

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 method:

@Override 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");
}

Is this some problem with OpenGL ES library? Does anyone have some advice on this? How to solve this? Thanks a lot

Was it helpful?

Solution

Change

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));
}

to

for (TouchEvent touchEvent : touchEvents){
    if(touchEvent.type != TouchEvent.TOUCH_UP)
    continue;
    game.setScreen(new MainMenuScreen(game));
}

This way you never risk to ask for a non existing object in a list. It simply won't run if the list is empty as in your case.

I don't know if it will solve your problem entirely; but you shuld get rid of the indexOutOfBOundsException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top