Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent LibGDX game from completely stopping when paused

In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?

like image 566
hasherr Avatar asked Feb 24 '14 02:02

hasherr


2 Answers

The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.

I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.

like image 158
Steven Trigg Avatar answered Sep 23 '22 10:09

Steven Trigg


You should use Asset Manager to prevent it, here a good tutorial : http://code.google.com/p/libgdx/wiki/AssetManager

the official doc : http://code.google.com/p/libgdx/wiki/AssetManager

Load all of your textures with assets manager

like image 34
LeSam Avatar answered Sep 21 '22 10:09

LeSam