Game Development Reference
In-Depth Information
@Override
public void create () {
// Set Libgdx log level
Gdx.app.setLogLevel(Application.LOG_DEBUG);
// Load assets
Assets.instance.init(new AssetManager());
// Start game at menu screen
setScreen(new MenuScreen(this));
}
}
Our platform-independent entry point of the game has obviously become quite
simple. Basically,
CanyonBunnyMain
has been reduced to only contain the
create()
method, which almost looks the same as before. What has changed inside this
method is that after setting the log level and loading our assets, LibGDX is instructed
through a call of the
setScreen()
method by the
Game
class to change the current
screen. As we want the game to start with the menu screen, we simply pass a new
instance of
MenuScreen
.
One last change is required to finish our preparations for multiple screen
management. The
WorldController
class holds our game logic and needs to initiate
a switch back to the menu whenever the player has lost the game or if either the
Esc
key or the back button is pressed.
Add the following two import lines to
WorldController
:
import com.badlogic.gdx.Game;
import com.packtpub.libgdx.canyonbunny.screens.MenuScreen;
After that, add the following code:
private Game game;
private void backToMenu () {
// switch to menu screen
game.setScreen(new MenuScreen(game));
}
This allows us to save a reference to the game instance, which will enable us to
switch to another screen. Additionally, a convenient method called
backToMenu()
has also been added that will switch to the menu screen when called.