Mateusz Bartkowski
Mateusz Bartkowski

Reputation: 729

When to use Screen, Stage, and Group in LibGDX

I have a main menu screen, which has 4 options, New Game, Load Game, Options and Exit.

Then:

New Game has more options such as difficulty, # of players + Start button.

Load Game has multiple saves to choose from together with Load and Delete options.

Options has Graphics level and Volume radio/ slider + Apply, Discard buttons.

At last, the game screen would have a pannable game window, together with an overlaying UI on the side of the screen.

My problem is, I don't know what class to use for those UI parts.

From this answer I understand that Screen is a full UI page, but it seems that creating a new Screen and then a Stage for each of those main menu options seems like an overkill, but maybe that's the way to go. I don't know if I should use Group and show/hide those depending on what the user clicks. I was also told to use Table to lay out the game screen. I'm utterly confused by all the guides I've found online. Every one seems different than the other.

The documentation is really good, but it never states how the individual parts are meant to be integrating with each other.

Is there any consensus on how to use those classes in LibGDX? Or is it a personal preference?

Upvotes: 0

Views: 1383

Answers (1)

AAryan
AAryan

Reputation: 20140

Screen is core libGdx part that represents one of many application screens, such as a mainscreen, a settingsscreen, the gamescreen and so on.

  • mainscreen may contains buttons like settings, play, level that help you to redirect on other screen.
  • settingsscreen may contain back button that redirect to previous mainscreen and other ui element like sound button, music button, language selection button.

It is hard to maintain different UI Screen in one ApplicationListenercontext with different required lifecycle.

so there is a Game class and Screen interface that helps to display one active screen at a time on device screen.

I think you've clear now, why we need different screens.


Now how I design one Screen Like MenuSceen that contains menu buttons, game name and similar element that act as HomeScreen.

You can use scene2d, a 2D scene graph for building UIs using a hierarchy of actors. Stage and Group are core classes of scene2d.

  • Stage class has a camera, SpriteBatch, and a root group and handles drawing the actors and distributing input events.

  • Group class is an actor that may have child actors.

Upvotes: 3

Related Questions