libgdx game how to display pause screen when user click on pause icon

风格不统一 提交于 2019-12-08 06:42:30

问题


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.


回答1:


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

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:

  1. 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.

  2. 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.

Some example 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.




回答2:


declare it

public static final int GAME_READY = 0; 

public static final int GAME_RUNNING = 1; 

public static final int GAME_PAUSED = 2; 

public static final int GAME_OVER = 4;

public static int state;

and in update method

            switch (state) {
    case GAME_READY:
        updateReady();
        break;
    case GAME_RUNNING:
        updateRunning(delta);
        break;
    case GAME_PAUSED:
        updatePaused();
        break;
    case GAME_OVER:
        gameOver = true;
        updateGameOver();
        break;
    }

This will definitely help you.




回答3:


    boolean GAME_PAUSED = false;


    if (GAME_PAUSED) {
    //Do not update camera
        batch.begin();
        resumeButton.draw(batch);
        batch.end();
    } else {
    //Update Camera
    Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    world.step(1/60f, 8, 3);
    camera.update();
    debugRenderer.render(world, camera.combined);
    //Do your game running
    }


来源:https://stackoverflow.com/questions/11466496/libgdx-game-how-to-display-pause-screen-when-user-click-on-pause-icon

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