I have a Manic Miner style game where you need to collect all the items from a screen before you can advance to the next screen.

I have a screen class called "PlayScreen" wich starts out by loading a tiled map called "Level1.tmx", and contains the render and update loop.

public class PlayScreen implements Screen {

This screen is loaded by my main Game class, by calling SetScreen

setScreen(new PlayScreen(this));

So inside my PlayScreen, what is the correct way to restart the game but with a different level? I know I can pass through a new level number to get it to load a new tiled map, but this usually crashes.

Do I need to dispose of the screen and reload it again. I cannot find much information about this. Most LibGDX tutorials only cover a single screen or level, or have a menu screen in between. I just want to switch to the next level or room on demand, but cannot get it working.

Has anyone done this before - can you recommend a good way of doing this?



  • \$\begingroup\$ Can you please give more information about the crashes you are getting? \$\endgroup\$ – Blunderchips Nov 24 '17 at 13:08

The Screen framework is designed so that you can change to another screen at any time. Your described method of doing do is correct-- by adding a parameter to your PlayScreen constructor

game.setScreen(new PlayScreen(this,levelNum));

The crash that you get when doing this is a different problem and you'll have to figure out where the crash is actually stemming from.

You should call dispose() on your Screens when you are done with them. In the example on the libgdx wiki they dispose of the screen right as they set the screen to another screen. So in your PlayScreen class you could have a restart method like this:

public void restart(int levelNum){
    game.setScreen(new PlayScreen(this,levelNum));

Just be sure that your Screen's dispose() method doesn't dispose any assets shared between screens. Shared assets should be passed into the screens and should be disposed at a later time, most likely when the game exits.

|improve this answer|||||
  • 1
    \$\begingroup\$ The dispose() in Game can be used to clear the shared items. \$\endgroup\$ – WeirdElfB0y Aug 23 '16 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.