LibGDX - 如何添加一个简单的按钮?

时间:2014-09-10 23:43:20

标签: libgdx

我对此感到非常困惑。不明白的是舞台和桌面布局是如何工作的。我想要的只有3个按钮,可以将我发送到另一个屏幕。有人可以写一个例子供我使用吗?这是我到目前为止的代码。

public class Menu implements Screen {
private SlingshotSteve game;

private Stage stage;
private TextButton button;
private TextButtonStyle textButtonStyle;
private BitmapFont font;

{

stage = new Stage(new ExtendViewport(800, 840));
Gdx.input.setInputProcessor(stage);

Table table = new Table();
table.setFillParent(true);
table.center().center();
stage.addActor(table);

font = new BitmapFont();
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
button = new TextButton("This is a button!!!", textButtonStyle);
stage.addActor(button);

}

// View Port Camera
private Viewport viewport;
PerspectiveCamera camera;


public Menu(SlingshotSteve gam) {
    this.game = gam;

}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    camera.update();
    game.batch.setProjectionMatrix(camera.combined);

    game.batch.begin();
    game.batch.end();


    if (Gdx.input.isTouched()) {
        game.setScreen((Screen) new GameScreen(game));
        dispose();
    }
}

@Override
public void resize(int width, int height) {
    // View Port Camera
    viewport.update(width, height); 

    stage.getViewport().update(width, height, true);


}

@Override
public void show() {

    // Viewport Camera
    camera = new PerspectiveCamera();
    viewport = new FitViewport(800, 480, camera);


}

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

}

1 个答案:

答案 0 :(得分:2)

不要将按钮添加到Stage。而是将其添加到您创建的Table

TextButton button1 = new TextButton("This is a button!!!", textButtonStyle);
TextButton button2 = new TextButton("This is a button!!!", textButtonStyle);
TextButton button3 = new TextButton("This is a button!!!", textButtonStyle);

table.add(button1);
table.row();
table.add(button2);
table.row();
table.add(button3);
table.row();