2016-04-26 32 views
1

我有一個帶有SpriteBatch的GameScreen來繪製我所有的紋理,但是我現在想在畫面上添加一個暫停按鈕。我試圖在我的SpriteBatch之上使用舞臺,但由於某種原因,它不會顯示出來。Hud的階段不可見

下面是一些代碼:

構造:

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(); 
} 

我在做什麼錯?

+0

你的setPosition pause_button.getWidth()的值是什麼?不應該是pause_image.getWidth()? – Hllink

+0

哦,是的,它應該是pause_image.getWidth(),我有我的代碼。 –

回答

0

不要在表中使用絕對位置,只是做這樣的事情:

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 (三角洲);和stage.draw();到batch.end()之後,GUI是最後一個應該呈現的東西,因此它可以位於所有東西的前面。

+0

嗯,是啊我現在試過了,舞臺仍然不會出現 –

+0

@Anton call table.setFillParent(true);創建表後,和stage.setDebugAll(true); – Hllink

+0

仍然沒有... –