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

10,950

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

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

Solution 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
    }
Share:
10,950
lakshman
Author by

lakshman

Updated on June 28, 2022

Comments

  • lakshman
    lakshman almost 2 years

    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.

  • vurp0
    vurp0 over 10 years
    You should use an enum instead, like so: public enum GameState { READY, RUNNING, PAUSED, GAMEOVER } public GameState state = GameState.READY;