I would first recomend you look into a few tutorial sites like Full Libgdx game tutorial or Games From Scratch.
Having said that, I will tell you how I handle game screens and level systems so that you can start at a level based from previous plays.
Libgdx comes with a Game class which you can extend for your application. This will be the starting point of your app. From this class you switch to different screens(Screen) depending on what task you're trying to accomplish, whether its changing the preferences, viewing high scores or playing the game.
Let's look at a single case that covers all the points in this question.
The user starts the app
The user is presented with a menu and chooses to view the options/prefs
The user goes back to the main menu
The user plays the game and dies at level 2
The end screen is shown to the user
The user is sent back to the main menu
The user plays again but starts at level 2
The user dies at level 3
The end screen is shown to the user
The user is sent back to the main menu
The user quits (and level 3 save point is saved)
Now we know how we want the process to play out we can start implementing this.
First our Game class which will control the changing of screens and saving of the current level. We also know we need a Screen for the menu, the game, the preferences and the end screen so we will add those in the Game class too. In our case shown above, this would be step 1.
MyGameClass
public class MyGameClass extends Game {
private MenuScreen menuScreen; // has a menu on it
private PreferencesScreen prefScreen; // has options to change game prefs
private GameScreen gameScreen; // shows the game
private EndGameScreen endScreen; // the end screen to show user points etc
// constants for different screens
public final static int MENU = 0;
public final static int PREFERENCES = 1;
public final static int APPLICATION = 2;
public final static int ENDGAME = 3;
// get the app preferences (for loading last level)
Preferences prefs = Gdx.app.getPreferences("My Preferences");
public int currentLevel = 1; // set the level to 1
@Override
public void create () {
// load the level from prefs file(if no value is saved 1 will be used)
currentLevel = prefs.getInteger("highlevel", 1);
// change the screen to the menu
changeScreen(MyGameClass.MENU);
}
// the method to change screen
public void changeScreen(int screen){
switch(screen){
case MENU :
menuScreen = new MenuScreen(this);
this.setScreen(menuScreen);
break;
case PREFERENCES :
prefs = new PreferencesScreen(this);
this.setScreen(prefs);
break;
case APPLICATION :
gameScreen= new GameScreen(this,currentLevel);
this.setScreen(gameScreen);
break;
case ENDGAME :
endScreen = new EndGameScreen (this);
this.setScreen(endScreen);
break;
}
}
public void saveLevel(){
prefs.putInteger("highlevel", currentLevel);
}
}
Now the Menu screen. This screen should basically show a menu so the user can click buttons to choose what they want to do, be it look at the preferences, start the game or quit the application. This is step 2-3,6 and 10
MenuScreen
public class MenuScreen implements Screen{
private MyGameClass parent;
public MenuScreen(MyGameClass mgc){
parent = mgc; //store the parent class
// so we can tell it to change screen
}
@Override
public void show(){
// code to show a menu and buttons etc
// I usually use Scene2D Stage and TextButtons
// in the button's listener I would change screen like this
parent.changeScreen(MyGameClass.APPLICATION);
}
}
Next the GameScreen which will show your game. This is the only screen that takes the parent class and an int value. The int value is to allow the game screen to choose which level to load. This is step 4, 7-8
GameScreen
public class GameScreen implements Screen{
private MyGameClass parent;
public GameScreen(MyGameClass mgc, int startLevel){
parent = mgc; //store the parent class
// load level goes here using startLevel to identify which level to load.
}
@Override
public void show(){
// code to show your game
}
@Override
public void render(float delta) {
// game code goes here and when player dies
// use parent.changeScreen(MyGameClass.ENDGAME);
// if the player gets to the next level
// update the parent level with
parent.currentLevel++;
// and save the current level to prefs
parent.saveLevel();
}
}
I won't go into details about the preferences screen as its explained in many guides. However the basic idea of this screen is:
PreferencesScreen
public class PreferencesScreen implements Screen{
@Override
public void show(){
// code to show your preferences controls
// in a button listener you would use
parent.changeScreen(MyGameClass.MENU);
// to go back to the menu
}
}
This should of covered how to switch between screens and allow the game to load certain levels based on previous plays by saving the level in the preferences file.