简体   繁体   中英

Switch screens after a button is pressed? Using LibGDX

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);

You should use setScreen method of Game class to change Screen.

playButton.addListener(new ClickListener() {

    @Override
    public void clicked(InputEvent event, float x, float y) {
    ((Game)Gdx.app.getApplicationListener()).setScreen(new   
     StartGameScreen());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM