0

I have a problem with my GameLoop in my Android projekt (my first Android projekt):

I have an Activity that the GameView (SurfaceView) starts.

setContentView(new GameView(this));

The GameView (SurfaceView) starts the GameThread.

    public void surfaceCreated(SurfaceHolder holder) {
    Log.d("GameView", "surfaceCreate");
    surfaceHolder = holder;
    synchronized (this) {               //Must be executed exclusively
        if (gameLoop == null) {
            gameLoop = new GameLoop();  //Start animation here
            gameLoop.start();
        }
    }
}

    private class GameLoop extends Thread {

    public boolean running = true;

    public void run() {
        Canvas canvas = null;
        final SurfaceHolder surfaceHolder = GameView.this.surfaceHolder;

        while (running) {
            try {
                canvas = surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    update();

                    checkCollision();

                    render(canvas);
                }
            } finally {
                if (canvas != null)
                    surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

}

And now I can play. Up to this point and everything is OK.

If the player loses a new Activity (GameOver) started and the GameThread stoped. Here he crashes!

    public void endgame() {

    Log.d("GameView", "ENDGAME");

    this.score.setStopscoring(true);
    this.box.stop();

    synchronized (surfaceHolder) {
        boolean retry = true;
        if (gameLoop != null) { 
            gameLoop.running = false;
            while (retry) {
                try {
                    gameLoop.join();
                    retry = false;
                } catch (Exception e) {
                }
            }
        }

    }

    Context context = getContext();
    context.startActivity(new Intent(context, _1GameOver.class));   

}

Afte the gameLoop.join(); it freezes.

I've tried a lot but nothing worked. Thank you before for your help

2 Answers2

0

I think the problem is that you are using two synchronized blocks, one to render (within the gameLoop), and the other to end it. The second block has to wait for the first one to stop executing ,which is never, hence the game freezes.

Try removing the " synchronized (surfaceHolder) " statement from the 2nd piece of code you posted.

Nullbeans
  • 309
  • 1
  • 8
0

I have not found the exact error.

Instead, I converted the code a bit.

    public void surfaceDestroyed(SurfaceHolder holder) {

        boolean retry = true;

        if (gameLoop != null) {

            gameLoop.running = false;

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

And everything I conclude where I close my Activity.

((_1GameActivity)getContext()).finish();

Not pretty but it works!

Pease out