How can I switch between different screens that implement Screen?

  • \$\begingroup\$ Draw the background then the buttons. Check if mouse clicked position was inside a button's rectangle (x, y, width, height). \$\endgroup\$ – dimitris93 Sep 8 '15 at 8:35
  • \$\begingroup\$ and then? that is just checking if the condition is true. \$\endgroup\$ – Kevin Bryan Sep 8 '15 at 9:24
  • \$\begingroup\$ That is what a menu is. The behavior after a button was clicked depends on your game and is for you to implement. All you need is a way to idenify which button was clicked and I explained briefly that you can check for Point (mouse position) inside Rectangle (button bounds). It is like asking what will happen if I press "q" in a game. In one game you might use a spell, in another you might quit the game. \$\endgroup\$ – dimitris93 Sep 8 '15 at 14:17

Something like below. Off course you need to add the Button to the stage, create a skin and the OptionScreen yourself.

    public void show() {
        final OptionScreen optionScreen = new OptionScreen();

        TextButton optionButton = new TextButton("Options", skin);

        optionButton.addListener(new ClickListener(){
            public void clicked(InputEvent event, float x, float y) {
                ((Game) Gdx.app.getApplicationListener()).setScreen(optionScreen);

Because we use optionScreen inside a listener here it has to be final since the application never knows when the button is clicked it. You can also just create a new instance of OptionScreen inline setScreen(new OptionScreen);.


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.