我有一个带有 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/