如何创建libgdx主菜单屏幕

时间:2015-09-08 07:39:25

标签: java menu libgdx

我是java和libgdx的新手,我想创建一个主菜单屏幕,有人可以给我一个简单的例子吗?

1 个答案:

答案 0 :(得分:18)

你所要求的是非常广泛的,它涉及许多元素,如创建按钮,皮肤,设置表等。无论如何你应该使用屏幕,添加一个舞台并添加演员到舞台。最终,您需要将Listeners添加到按钮actor以切换屏幕。这是我为你做的一个:

public class TestScreen implements Screen{

    private SpriteBatch batch;
    protected Stage stage;
    private Viewport viewport;
    private OrthographicCamera camera;
    private TextureAtlas atlas;
    protected Skin skin;

    public TestScreen()
    {
        atlas = new TextureAtlas("skin.atlas");
        skin = new Skin(Gdx.files.internal("skin.json"), atlas);

        batch = new SpriteBatch();
        camera = new OrthographicCamera();
        viewport = new FitViewport(Constants.WorldWidth, Constants.WorldHeight, camera);
        viewport.apply();

        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
        camera.update();

        stage = new Stage(viewport, batch);    
    }


    @Override
    public void show() {
        //Stage should controll input:
        Gdx.input.setInputProcessor(stage);

        //Create Table
        Table mainTable = new Table();
        //Set table to fill stage
        mainTable.setFillParent(true);
        //Set alignment of contents in the table.
        mainTable.top();

        //Create buttons
        TextButton playButton = new TextButton("Play", skin);
        TextButton optionsButton = new TextButton("Options", skin);
        TextButton exitButton = new TextButton("Exit", skin);

        //Add listeners to buttons
        playButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ((Game)Gdx.app.getApplicationListener()).setScreen(new PlayScreen());
            }
        });
        exitButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit();
            }
        });

        //Add buttons to table
        mainTable.add(playButton);
        mainTable.row();
        mainTable.add(optionsButton);
        mainTable.row();
        mainTable.add(exitButton);

        //Add table to stage
        stage.addActor(mainTable);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(.1f, .12f, .16f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
        camera.update();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        skin.dispose();
        atlas.dispose();
    }
}

我称之为的方法是稍微更改初始类。

//Let the class extend from game
public class MyGame extends Game()
{
    //Delete everything in it and leave a create() with a single line
    @Override
    public void create() {
    setScreen(new MenuScreen());
    }
}

当然,要使上述代码正常工作,您需要设置SkinAtlas来绘制按钮。但是,您可以只添加图像和字体并手动创建按钮。无论如何,我只是answered a question,我正在深入探讨如何创建SkinAtlas

编辑虽然已经询问了菜单类的示例,但用户询问者实际上只需知道如何从屏幕切换到屏幕。有点尴尬但是不可思议地写了上面的代码只花了几分钟;)。

您始终可以使用ApplicationListener从任意位置访问Gdx.app.getApplicationListener。您可以将其转换为Game以访问setScreen

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

或者您可以手动传递初始Game对象或applicationListener。确保新屏幕接受游戏对象。

public class MenuScreen
{
    private Game gameObject;
    public MenuScreen(Game gameObject)
    {
        this.gameObject = gameObject;
    }
    private void someMethod()
    {
         //Switches to a new MenuScreen... 
         //useless in most cases but you get the idea
         gameObject.setScreen(new MenuScreen(gameObject);
    }
}