java - Hud 的舞台不可见

标签 java libgdx

我有一个带有 SpriteBatch 的 GameScreen 来绘制所有纹理,但我现在想在屏幕上添加一个暂停按钮。我尝试在 SpriteBatch 之上使用舞台,但由于某种原因它不会显示。

下面是一些代码:

构造函数:

public GameScreen(MyGame game) {

    stage = new Stage();
    table = new Table();

    pause_texture = new Texture("path_to_texture.png");
    pause_image = new Image(pause_texture);
    pause_image.setPosition(1920 - pause_image.getWidth() - 20, 20);

    table.add(pause_image);
    stage.addActor(table);
}

渲染方法:

public void render(float delta) {
    Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    stage.act(delta);
    stage.draw();

    game.camera.update();

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

    game.batch.begin();
    //rendering stuff here
    game.batch.end();
}

我做错了什么?

最佳答案

不要在表格上使用绝对位置,只需执行以下操作:

public GameScreen(MyGame game){

    stage = new Stage();
    table = new Table();

    pause_texture = new Texture("path_to_texture.png");
    pause_image = new Image(pause_texture);

    table.add(pause_image);
    table.bottom().right();
    stage.addActor(table);

}

如需进一步引用,请查看Table在 Libgdx 维基上

别忘了移动 stage.act(delta);和 stage.draw();在batch.end()之后,GUI是最后应该渲染的东西,因此它可以位于所有东西的前面。

关于java - Hud 的舞台不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866385/

相关文章:

java - 制作隐形 body 线 box2d libgdx

java - 更改 Eclipse 的编码字符

java - 正则表达式获取字符串中的第一个数字和其他字符

java - 如何恢复 Oracle JDK 更新?

java - 为什么示例作者对正交相机的宽度和高度进行硬编码?(LibGdx Zombie Bird 教程)

libGDX InputListener、InputProcessor、InputAdapter有什么区别

java - 在 Libgdx 中使用屏幕时重用代码

java - Collections.copy问题

java - 如何使用 JButton 和 JTextArea 不断向列表添加值而不重写它们

android libgdx 与 Actor 一起拍摄