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

时间:2016-06-28 19:27:49

标签: android menu libgdx

我想知道如何将屏幕从主菜单更改为游戏本身,但我遇到了麻烦,我很感激帮助。我想要做的是当我按 btn.Play 时,它会将屏幕从主菜单更改为游戏。

这是我的代码:

游戏:

@Override
public void create(){

    batch = new SpriteBatch();

    Up = new Texture("left.png");

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

    //creates the square in the middle of the screen
    square = new Rectangle();
    square.x = 630;
    square.y = 720 / 2 - 32 /2;
    square.width = 32;
    square.height = 32;

    //code
}

//code

public void render() {

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

    cam.update();

    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    batch.draw(Up, square.x, square.y);
    //code
    batch.end();
}

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

public class Screens extends com.badlogic.gdx.Game {


//creates the timing on the splash screen


@Override
public void create() {

    startTime = TimeUtils.millis();

    //sets the next screen as the splash screen
    setScreen( new SplashScreen(this));
}

@Override
public void render(){

    super.render();
    rendCount++;
}

@Override
public void dispose(){

    endTime = TimeUtils.millis();
}

闪屏:

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

}

使用时在MainMenu类中:

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

        }
    });

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

0 个答案:

没有答案
相关问题