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