MAGS94 MAGS94 - 1 year ago 462
Android Question

Pause and Resume in Libgdx

In my game when I press menu key and then back to the game it continues from where it paused
e.g. if I have a sprite moving to 0, 0 from full screen width and height and stopped at width/2 and height/2 it will start from this point and so on other things like music which stops then continues although I do nothing in pause and resume

My question is what should I do inside

of screens or main game class or should I leave them empty as they are ?

In main game class
I call

I am loading my assets in
should I do thing with this ?

Answer Source

When you press the home button, your game is not rendering until you open the game again. So your sprite moves on (is beeing rendered) at the same position because the reder() method was not beeing called while you left the game, thus not changing the coordinates of your sprite(s).

The pause() method is called on Android when the Home button is pressed or an incoming call is received. You could leave this method empty or for example save your game status to file.

The resume() method is called when the application resumed from pause state, that means if you open it again. You could also leave this method empty or for example show an options menu, so that the user knows that the game paused while he went out of the game.

For further information on the life cycle and states of a libgdx application, check the official documentation for this.

You could load your assets at the application start up (maybe you should use an AssetManager) in the main class (which extends Game) and pass a reference of the assets/AssetManager from the main class to every new Screen class you instantiate. This link in the libgdx wiki explains the act of managing assets.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download