0

I have problem with resuming my game after pause. I'm using SurfaceView, and here is the most important part of code:

void pause() {
    if (!pause) {
        gameLoopThread.setRunning(false);
        pause = !pause;
        System.out.println("pause");
    }
    else {
        gameLoopThread.setRunning(true);
        System.out.println("end pause");
        pause = !pause;
    }
}

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);

    getHolder().addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);

            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {}
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            //createSprites();

            gameLoopThread.setRunning(true);
            gameLoopThread.start();
            thread1.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                                    int width, int height)
        {
        }

When I use pause first time, game is stopping, but when I'm trying to resume game by using pause second time it doesn't work, but I have "end pause" printed in log.

sigod
  • 3,514
  • 2
  • 21
  • 44
pjp
  • 157
  • 1
  • 12

1 Answers1

0

Normally u don't pause the whole thread to have a pause in ur game. As ur update, render should normally have anykind of delta time object, u should implement a new component to ur delta time object, called something like time scale. The time scale just muliplies time delta by itself to return the modified delta time. So now if u want to pause ur game, just set time scale to 0, and ur done nothing moves anymore, and u can still have a running update, render queue to modify things, animations or anything else.

Another method could be, to give every entity, let's say in ur base entity, a paused property. Then u send through ur entities a message let's say pausegame and the paused property get's set to true. In the virtual update of ur base entity just check for paused and if set return.

The secound one is a more advanced one with some propper advantages over the first one, but need at least a well design of ur entity/component concept if it even exists.

Yosh Synergi
  • 314
  • 3
  • 12