0

In my almost finished game I want to set a new screen on game over. I got my game over method:

    private void gameOver(Label score) {
    for (Body body : worldBodies) {
        world.destroyBody(body);
    }
    dispose();
    ((Game) Gdx.app.getApplicationListener()).setScreen(new GameOver());
}

Whenever the method gets called the screen freezes for a second and the game crashes with this statement:

JNI DETECTED ERROR IN APPLICATION 
09-02 22:04:34.036      391-425/com.joelbrun.jetskirider.android A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0x8 in tid 425 (GLThread 29501)
jobr97
  • 179
  • 1
  • 17

1 Answers1

2

Try updating your code to match the following:

private void gameOver(Label score) {
    for (Body body : worldBodies) {
        world.destroyBody(body);
    }
    dispose();
    myGame.setScreen(new GameOver());
}

The first problem was that you were disposing/loading the GameOver screen inside of your loop instead of after it.

The second problem was with your call to Gdx.app.getApplicationListener(). Every time you call that it returns a new instance of your Game. Instead you need to hold on to a reference to your game and instead use that.

If you still get the same error, make sure you are calling dispose() appropriately. If resources are disposed of before they have been released then you can get some pretty nasty crashes.

Jim Buck
  • 2,383
  • 23
  • 42
  • its still crashing with: JNI DETECTED ERROR IN APPLICATION – jobr97 Sep 02 '15 at 20:32
  • @JoelBrun What is the rest of the exception? – Jim Buck Sep 02 '15 at 20:34
  • So it looks like a parent process has died out, which is why you are getting a [`DeadObjectException`](http://developer.android.com/reference/android/os/DeadObjectException.html). Does it work when you don't call `dispose()`? Possibly look into [this post](http://stackoverflow.com/a/1574355/1654818). – Jim Buck Sep 02 '15 at 20:46
  • It also crashes without the dispose – jobr97 Sep 02 '15 at 20:48
  • Then when does it crash? When a `Body` is destroyed or sometime during the call to `setScreen`? – Jim Buck Sep 02 '15 at 20:50
  • Its during the setScreen – jobr97 Sep 02 '15 at 20:51
  • See this thread for more details: http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=12869 – Jim Buck Sep 02 '15 at 20:57
  • I dont really understand what I have to do... where do I have to put this code mentioned in the thread? @Jimmy – jobr97 Sep 03 '15 at 16:59
  • I have no clue what your application structure is, but I've typically done one of two things. The first is to store the `Game` instance in a static property on a static class, something like `GameVariables.Game`. Then you can reference that anywhere, but it's not very clean. The better option is wherever you instantiate your `Game` object pass that instance into each constructor for every single `Screen`/entity ([shown here](https://code.google.com/p/libgdx-users/wiki/ScreenAndGameClasses)). – Jim Buck Sep 03 '15 at 17:04
  • I did what the link said but it still crashes – jobr97 Sep 04 '15 at 16:34
  • Hmm, sorry I couldn't help more, maybe someone else has had similar issues! – Jim Buck Sep 06 '15 at 02:26
  • I just found it out! It was because I called gameOver() out of beginContact () which is an implemented class – jobr97 Sep 06 '15 at 06:59