Skip to content

Preserving Screens

Lucas Ryall edited this page Aug 22, 2024 · 2 revisions

Creating New Screens Screens Without Loosing Current Screen

New screen can be created without having to dispose of the current screen.

How To?

Creating New Screen

In the GdxGame.java (source\core\src\main\com\csse3200\game\GdxGame.java), first add a method following this format:

  public void add<YOUR_SCREEN>() {
    addScreen(ScreenType.<YOUR_SCREEN_NAME>, getScreen());
   }

Second, add <YOUR_SCREEN_NAME> to the ScreenType enum.

Thirdly add another case to the switch statement in the newScreen method in the following format:

case <YOUR_SCREEN_NAME>:
    return new <YOUR_SCREEN_CLASS>(this, screen, container);

Don't forget to import the class for your screen.

Returning to Old Screen

All screens will have a reference to the the GdxGame instance game, as all are initialised with it. In order to return to the Screen you have stored, call the setOldScreen(screen, container) method, with screen being the Screen instance of the screen you are returning to, and container being the ServiceContainer instance that was passed to your current screen's constructor. This will completely dispose of your current screen, and bring the screen you stored back.

Note that it is probably worth adding a getGame method to your screen, and then add a listener to the for this method through the ServiceLocator class so that you can get the game from anywhere, and hence be able to trigger returning to the previous screen from anywhere, and not have to pass around a reference to the game to each object.

Clone this wiki locally