LibGDX - Scene2D based level selection screen including locking of levels?

Hi guys

I’m working on an Android game but i’m a little stuck on how to create the level selection screen. Now I plan on placing some Scene2D text buttons inside a table and then placing that onto the stage. So on the first page, 9 levels might be shown. How would I go about allowing the user to scroll over to the next set of levels? Would I simply create a stage that is twice the width of the screen and then pan the camera?

Also how would I implement a star based completion system? So telling each level how many stars the user got and then feeding that information into Scene2D to display and deciding if the next level can be unlocked

I’ve got a YouTube video below that shows what i’m trying to explain

You can use:

  • Stage > Scene2D > ScrollPane (if need scroll) > Table > Buttons > StarsLayout on Buttons (button.add(starTable).height(30))

This is a super old tutorial and need to be adapted, but I think can give you some idea. http://nexsoftware.net/wp/2013/05/09/libgdx-making-a-paged-level-selection-screen/

You can fill all the cells programatically and lock/unlock stars or levels with a saved file, like Preferences in libGDX.

Thank you Craft, I was heading in that direction :). Will I manually have to add a listener to every single level button? Or is there a better way of handling which level is selected and then loading up that leevel

In my game I did something similar (without the stars), I had 40 characters to create a selection screen (with lock/unlock), so create 40 specific listeners is bad, that was my solution.

  • I verify which characters are unlocked in Preferences to create the buttons.
  • When the button is created you can set a name, so you can use this value to know the selected character:
{...
button.setName("character_name");
button.addListener(characterClickListener); 
table.add(button);
table.row();
...}

public ClickListener characterClickListener = new ClickListener() {
	        @Override
	        public void clicked (InputEvent event, float x, float y)
	        {
	            System.out.println("Click: " + event.getListenerActor().getName()); //print    Click: character_name
	        }
	    };
  • You can use this value to know the next screen or send as param to create the next screen based on value.

Instead of use a name, you can use an integer, list, cell or any other method to know which button was clicked and use this value to anything else.

Thank you very much! :slight_smile: This is exactly what I have done.

I gave each button a name via my for loop and then created an inner class that handles all the input. I was going to use an anonymous class but I think they look ugly :stuck_out_tongue:

I’ve read online that instead of a switch statement to change the level, I could use something like


String className = event.getListenerActor().getName();
Class selectedLevel = Class.forName(locationOfClassHere + className)
selectedLevel.getDeclaredConstructors();

Is this really a safe alternative? It seems that it would save a lot of time and every time I create a new level, I don’t need to change the switch code :slight_smile: