Skip to content

Screens

miguel edited this page May 10, 2020 · 8 revisions

The concept of a screen is already implemented in libgdx. They control the menu flow of your game. Screens give players access to various game features and menus, such as main menu, credits, settings or ingame features. Let us create a first screen in the core project of your libgdx game:

public class MyScreen extends BrainGdxScreen2D {

    // Required constructor by brainGDX
    public MyScreen (BrainGdxGame game) {
	super(game);
    }

    @Override
    protected void onCreate(GameContext2D context) {
        // is called when the screen stage is created.
    }
}

Now since we have created our first screen we have to register it to brainGDX. To use brainGDX properly you have to define your main game class in your core project as follows:

public class MyGame extends BrainGdxGame {

    @Override
    protected GameAssetLoader getAssetLoader() {
	return new EmptyAssetLoader();
    }

    @Override
    protected AbstractScreen<?> getInitialScreen() {
	return new MyScreen(this);
    }
}

Ignore the getAssetLoader() method for now. If you want to learn more about asset loading in braingdx, continue reading here. More importantly is the getInitialScreen() method which will be called by brainGDX on startup. This defines the very first screen your game should start with. Start your game as a Desktop application to see the result.

You may have noticed that the screen is still empty! To show things on the screen, move on and learn how to render entities onto the screen.

Clone this wiki locally