libgdx游戏如何显示画面停顿时暂停图标用户点击图标、画面、用户、游戏

2023-09-08 08:49:23 作者:云归处

在libgdx如何显示画面停顿时停顿的用户点击,它应该是在present屏蔽层,它应该关闭屏幕上时,用户简历点击我怎样才能在libgdx实现它。

In libgdx how show pause screen when user click on pause and it should be layer over the present screen and it should close the screen when user click on resume how can i implement it in libgdx.

推荐答案

我不喜欢这里使用Android原生景观的建议,这可以整齐地内部完成libgdx本身。

I don't like the suggestion about using native Android View here, this can be done neatly inside libgdx itself.

我将不得不一些变量定义游戏的当前状态。如果暂停按钮为pressed,或游戏由Android暂停(例如,如果用户presses home键)这个变量应该得到一个暂停的值。然后在你的渲染()方法,如果这个变量有停顿的价值,你得到一些暂停屏幕。

I would have had some variable that defines the current state of the game. If pause button is pressed, or the game is paused by android (for instance if the user presses the home button) this variable should get a paused value. Then in your render() method, if this variable has the value of pause you draw some pause screen.

这个屏幕可以以多种方式绘制。如果您使用的是一个舞台,你有两大选择:

This screen can be drawn in multiple ways. If you are using a Stage, you have two great options:

如果停下来,除了游戏的阶段,绘制暂停项圣人绘制的游戏阶段之后。那么这将是游戏的顶部。

If paused, in addition to the game-stage, draw a sage with pause-items after you draw the game-stage. Then it will be on top of the game.

您可以创建一些窗口演员,并添加暂停项给它。然后,当游戏暂停,你添加/让它在你的舞台可见。

You can create some Window actor, and add the pause-items to it. Then when the game is paused you add it/make it visible in your stage.

一些示例code:

public class GameScreen implements Screen {

    private Stage mystage;

    public static final int GAME_RUNNING = 0;
    public static final int GAME_PAUSED = 0;

    private int gamestatus;

    // ...

    public void render(float deltaTime) {
        // draw game normally, probably shouldn't update positions etc. if 
        // the game is paused..

        if (pausebutton is pressed) {
            pauseGame();
        }

        if (gamestatus == GAME_PAUSED) {
            // draw pause screen
        }

    }   
    public void pauseGame() {
        gamestatus = GAME_PAUSED;
    }

    // this is called by android 
    public void pause() {
        pauseGame();
    }
}

不是一个完全工作的例子,但显示的基本逻辑。

Not a fully working example, but showing the basic logic.