0
\$\begingroup\$

Currently working on a game for school and I ran into a problem. I created a Main Menu for my game with the buttons (Play, Level Picker, and Settings). What I'm trying to do is when the "Play" is pressed it will switch to a different screen where the user will play. The menu is under the name of MainMenu and the screen I want it to switch to is called StartGameScreen. I've tried looking online for my answer but can't seem to find something that will work. If it helps here's my code for the play button. Any help would be fantastic!

TextButton playButton = new TextButton("Play", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin
playButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 + (playButton.getHeight() + buttonOffSet));
playButton.addListener(new ClickListener() {

    @Override
    public void clicked(InputEvent event, float x, float y) {
        System.out.println("play game button clicked");
    }
});
stage.addActor(playButton);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Welcome to Stack Exchange!

A good reference for you to look at would be the libGDX wiki. Chances are it will answer 80% of your questions. Its "A Simple Game" tutorial and "Extending the Simple Game" tutorial walks through what you need (the latter shows how to switch screens).

The general pattern that you'll see though, is that when you have a screen class, you'll pass it an instance of your Game extension like so:

class MyScreen implements Screen {
  private Game parent;
  public MyScreen(Game parent) {
    this.parent = parent;
  }
  // Other stuff clipped due to space reasons
}

Then when you need to change to a different screen, you call Game's setScreen() method to change to a new screen. If you find that setScreen() doesn't do what you want, you can use the same pattern but create your own version of the setScreen() method.

\$\endgroup\$

You must log in to answer this question.

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