intellij-idea 如何使用libgdx和java创建主菜单

g2ieeal7  于 2022-11-01  发布在  Java
关注(0)|答案(1)|浏览(115)

在过去的几天里,我一直在玩libgdx,我想看看是否有人能给予一个简单的例子,说明如何使用开始按钮和退出按钮创建主菜单屏幕。

ldxq2e6h

ldxq2e6h1#

LibGDX建议如果您需要构建UI,可以使用它的“scene 2d”组件。
最简单的例子可以在LibGDX的Wiki上找到,就在关于scene 2d的文章中:https://libgdx.com/wiki/graphics/2d/scene2d/scene2d
实际上,wiki中的第一个代码示例就可以了--我对它进行了一些修改,应该将其添加到Screen或Game的实现中:

private Stage stage;

@Override
public void create () {
    stage = new Stage(new ScreenViewport());
    Gdx.input.setInputProcessor(stage);

    // Insert your UI elements here, for example:
    Skin skin = new Skin(Gdx.files.internal("skin.json")); // https://libgdx.com/wiki/graphics/2d/scene2d/skin
    Table menuContainer = new Table();
    TextButton playButton = new TextButton("Play", skin);
    playButton.addListener(new ClickListener() {
        @Override
        public void clicked (InputEvent event, float x, float y) {
            // Called when player clicks on Play button
        }
    });
    menuContainer.add(playButton);

    TextButton exitButton = new TextButton("Exit", skin);
    exitButton.addListener(new ClickListener() {
        @Override
        public void clicked (InputEvent event, float x, float y) {
            // Called when player clicks on Exit button
        }
    });
    menuContainer.add(exitButton);
}

@Override
public void resize (int width, int height) {
    // See below for what true means.
    stage.getViewport().update(width, height, true);
}

@Override
public void render () {
    float delta = Gdx.graphics.getDeltaTime();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();
}

@Override
public void dispose () {
    stage.dispose();
}

相关问题