简体   繁体   English

按下按钮后切换屏幕? 使用LibGDX

[英]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. 该菜单位于MainMenu的名称下,我希望其切换到的屏幕称为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. 您应该使用Game类的setScreen方法更改Screen。

playButton.addListener(new ClickListener() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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