Android 如何使用libGDX将屏幕从主菜单更改为游戏?

Android 如何使用libGDX将屏幕从主菜单更改为游戏?,android,menu,libgdx,Android,Menu,Libgdx,我想知道如何将屏幕从主菜单切换到游戏本身,但我遇到了很多麻烦,我将非常感谢您的帮助。我想做的是当我按下btn。播放它会改变屏幕从主菜单到游戏 以下是我的代码: 游戏: 屏幕(用于显示第一个屏幕): 溅屏: public class SplashScreen implements Screen { public SplashScreen(Game g) { myGame = g; cam = new OrthographicCamera(); cam.setToO

我想知道如何将屏幕从主菜单切换到游戏本身,但我遇到了很多麻烦,我将非常感谢您的帮助。我想做的是当我按下btn。播放它会改变屏幕从主菜单到游戏

以下是我的代码:

游戏:

屏幕(用于显示第一个屏幕):

溅屏:

public class SplashScreen implements Screen  {


public SplashScreen(Game g) {

    myGame = g;

    cam = new OrthographicCamera();
    cam.setToOrtho(false, 1280, 720);
}


@Override
public void show() {


    //image of the splash screen
    splashTexture = new Texture("splash.jpg");

    //timer starts
    timeStart = TimeUtils.millis();
}

@Override
public void render(float delta) {

    SpriteBatch batch = new SpriteBatch();

    //draws the image on screen
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    batch.draw(splashTexture, 0, 0);
    batch.end();
    rendCounter++;

    // if the timer is higher than 5 secs goes to the main screen
    if(TimeUtils.millis() > (timeStart+5000)) {
        myGame.setScreen(new MainScreen());
    }

}

@Override
public void dispose(){

    splashTexture.dispose();

}
主菜单:

public class MainScreen implements Screen{

private SpriteBatch batch;
private OrthographicCamera cam;
private Stage stage;
private TextButton btnPlay, btnExit;
private BitmapFont font;
private Skin skin;
private TextureAtlas atlas;

public MainScreen() {

    cam = new OrthographicCamera();
    cam.setToOrtho(false, 1280,720);
    batch = new SpriteBatch();

}


@Override
public void show() {

    // creates the pack for the buttons, its skin, the font used on the buttons, and the stage(screen)

    atlas = new TextureAtlas("buttons.pack");
    skin = new Skin();
    skin.addRegions(atlas);
    font = new BitmapFont(Gdx.files.internal("font.fnt"), false);

    stage = new Stage();
    stage.clear();
    Gdx.input.setInputProcessor(stage);

    // sets the style of the buttons

    TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
    style.up = skin.getDrawable("btn_up");
    style.down = skin.getDrawable("btn_down");

    // sets the font of the buttons

    style.font = font;


    //creates the play button with the text, its position and the size

    btnPlay = new TextButton("Play", style);
    btnPlay.setSize(350, 150);
    btnPlay.setPosition(450, 400);

        btnPlay.addListener(new InputListener() {

            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

                ((Game) Gdx.app.getApplicationListener()).setScreen(new Game());
                dispose();

            }
        });

    btnExit = new TextButton("Exit", style);
    btnExit.setSize(350,150);
    btnExit.setPosition(450, 150);

    btnExit.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            Gdx.app.exit();
            dispose();

        }
    });

    stage.addActor(btnPlay);
    stage.addActor(btnExit);

}

@Override
public void render(float delta) {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(0, 0, 0, 1);

    batch.setProjectionMatrix(cam.combined);

    stage.act();

    batch.begin();
    stage.draw();
    batch.end();

}

@Override
public void dispose() {
    batch.dispose();
    stage.dispose();
    atlas.dispose();
    skin.dispose();
    font.dispose();

}
在主菜单类中,当我使用:

btnPlay.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            ((Game) Gdx.app.getApplicationListener()).setScreen(new Game());
            dispose();

        }
    });

它使应用程序崩溃。有什么建议吗?

可能是重复的吗?我一直在研究一些类似的问题,似乎找不到解决我的问题的方法,只是需要有人来检查我可以改变什么以及如何改变。这看起来像是一团糟,不是吗?你们要做的第一件事就是找出导致你们的应用程序崩溃的确切原因,这样你们就可以从那个里进行调试了。我想你已经把范围缩小到了几行了。为什么MainMenu类不像SplashScreen类那样在构造函数中获取游戏对象?然后在你的修补方法中,你会将屏幕设置为一个新的游戏屏幕。您正在将其设置为“新游戏()。游戏对象实际上什么都不做。逻辑都在屏幕上。你需要为游戏创建一个新的屏幕,并将屏幕设置为该屏幕。@Barodapride我对主菜单类做了与SplashScreen类相同的操作,它不再崩溃,尽管当我按下play按钮时,它只会显示一个黑屏,而不是游戏本身。你必须给它一个真正的屏幕。你不能给它一个“游戏”。可能是重复的我一直在看一些类似的问题,我似乎找不到解决我的问题的办法,只是需要有人来检查我可以改变什么和如何。嗯,这看起来像一团乱,不是吗?你们要做的第一件事就是找出导致你们的应用程序崩溃的确切原因,这样你们就可以从那个里进行调试了。我想你已经把范围缩小到了几行了。为什么MainMenu类不像SplashScreen类那样在构造函数中获取游戏对象?然后在你的修补方法中,你会将屏幕设置为一个新的游戏屏幕。您正在将其设置为“新游戏()。游戏对象实际上什么都不做。逻辑都在屏幕上。你需要为游戏创建一个新的屏幕,并将屏幕设置为该屏幕。@Barodapride我对主菜单类做了与SplashScreen类相同的操作,它不再崩溃,尽管当我按下play按钮时,它只会显示一个黑屏,而不是游戏本身。你必须给它一个真正的屏幕。你不能给它一个“游戏”。
public class MainScreen implements Screen{

private SpriteBatch batch;
private OrthographicCamera cam;
private Stage stage;
private TextButton btnPlay, btnExit;
private BitmapFont font;
private Skin skin;
private TextureAtlas atlas;

public MainScreen() {

    cam = new OrthographicCamera();
    cam.setToOrtho(false, 1280,720);
    batch = new SpriteBatch();

}


@Override
public void show() {

    // creates the pack for the buttons, its skin, the font used on the buttons, and the stage(screen)

    atlas = new TextureAtlas("buttons.pack");
    skin = new Skin();
    skin.addRegions(atlas);
    font = new BitmapFont(Gdx.files.internal("font.fnt"), false);

    stage = new Stage();
    stage.clear();
    Gdx.input.setInputProcessor(stage);

    // sets the style of the buttons

    TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
    style.up = skin.getDrawable("btn_up");
    style.down = skin.getDrawable("btn_down");

    // sets the font of the buttons

    style.font = font;


    //creates the play button with the text, its position and the size

    btnPlay = new TextButton("Play", style);
    btnPlay.setSize(350, 150);
    btnPlay.setPosition(450, 400);

        btnPlay.addListener(new InputListener() {

            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

                ((Game) Gdx.app.getApplicationListener()).setScreen(new Game());
                dispose();

            }
        });

    btnExit = new TextButton("Exit", style);
    btnExit.setSize(350,150);
    btnExit.setPosition(450, 150);

    btnExit.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            Gdx.app.exit();
            dispose();

        }
    });

    stage.addActor(btnPlay);
    stage.addActor(btnExit);

}

@Override
public void render(float delta) {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(0, 0, 0, 1);

    batch.setProjectionMatrix(cam.combined);

    stage.act();

    batch.begin();
    stage.draw();
    batch.end();

}

@Override
public void dispose() {
    batch.dispose();
    stage.dispose();
    atlas.dispose();
    skin.dispose();
    font.dispose();

}
btnPlay.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            ((Game) Gdx.app.getApplicationListener()).setScreen(new Game());
            dispose();

        }
    });