3
\$\begingroup\$

I've been having issues with Screens, as I want to switch between the main game screen, the battle screen and the ingame menu screen (JRPG). The problem is that I have initialised all the information (Position of Player and NPC's, Map data, Sava data, etc) in the Show() function of the screen. That means that when I switch between 2, it will rebuild and reload everything all over again. There is also no Create() function in Screen so I'm at a dead end right now. Is there a way I can reserve data or draw the other screen on top of the main game screen, pausing that one?

I really need help with this.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Don't initialize everything in show. Consider passing a reference of the parent screen to the child screen along with a reference to your Game instance when creating new screens. That way the child screen has a way to get back to an already initialized version of the parent screen.

public class MyLibGdxGame extends Game {
  @Override
  public void create () {
    MainScreen mainScreen = new MainScreen();
    setScreen(mainScreen);
  }
}

public class MainScreen implements Screen {
  private final MyLibGdxGame game;

  public MainScreen(MyLibGdxGame game) {
    this.game = game;
    // Setup and load stuff here
  }

  @Override
  public void render(float delta) {
    // Update and render your game state as per usual here

    if (should_show_battle)
      game.setScreen(new BattleScreen(game, this)); 
  }
}

public class BattleScreen implements Screen {
  private final MyLibGdxGame game;
  private final Screen parent;

  public BattleScreen(MyLibGdxGame game, Screen parent) {
    this.game = game;
    this.parent = parent;
    // Initialize battle screen here
  }

  @Override
  public void render(float delta) {
    // Update and render your game state as per usual here

    if (should_return_to_parent) {
      // As parent here is already initialized, there's no need to reload 
      // or setup anything
      game.setScreen(parent); 
    }
  }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .