Drawing pause Screen as a layer over the play screen-LibGdx

谁说胖子不能爱 提交于 2019-12-20 05:49:07

问题


In my LibGdx Game I created pause functionality.While playing game,If I press pause button,a separate screen with resume button displays.

What actually I want to do is that the pause screen should appear above the game play screen like a layer.

Like the screen shot of a game below:

I could only make a new pause screen in my game with separate bg and all. I want to display the paused game as it is.Above that I can draw graphics for resume button,box etc exactly like the screen shot.

How can I acheive this layer like display of pause screen?


回答1:


Inside Gamescreen, you're using stage for UI(buttons to navigate). I am pretty sure you're doing like this.

private uiStage;
private boolean isPause;
private Group pauseGroup; 

public void render(){

    if(!isPause) UpdateGame();    
    DrawGame();         

    uiStage.act();
    uiStage.draw();
}

Create a Group whenever you want to pause your game and remove that group when you want to resume your game. You can also crate your Group once and add all pause UI in show() method. Add that group to stage when you want to show pause and remove when you resume your game.

public void pauseGame(){

   isPause = true;
   pauseGroup = new Group;
   Image semiTransparentBG= ......
   // setSize(Size of screen) and make it semi transparent.
   pauseGroup.addActor(semiTransparentBG);

   //crate all other pause UI buttons with listener and add to pauseGroup

   stage.addActor(pauseGroup);

}

public void resumeGame(){

    if(isPause){
      isPause=false;
      pauseGroup.remove();
    }  
}



回答2:


Short answer: yes, you can. First let your pause screen extend the Stage class instead of the Screen class. Then try calling the draw of the pause screen after the draw of the game. Something like this:

public void render(){
    UpdateGame();
    pause_screen.act();

    DrawGame();
    pause_screen.draw();
}

Stop the game updates when the pause screen is open. Off course this is over simplified, but you can look in to it.



来源:https://stackoverflow.com/questions/42578795/drawing-pause-screen-as-a-layer-over-the-play-screen-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!