我有我的GameLoop在我的Android PROJEKT一個問題(我的第一款Android PROJEKT):的Android Gameloop:遊戲結束,並開始活動
我的GameView(SurfaceView)開始一個活動。
setContentView(new GameView(this));
GameView(SurfaceView)啓動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);
}
}
}
}
現在我可以玩了。到目前爲止,一切都很好。
如果玩家失去一個新的Activity(GameOver)開始並且GameThread停止。 他在這裏崩潰!
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));
}
未完成gameLoop.join();
它凍結。
我試了很多,但沒有任何工作。 前,感謝你的幫助
感謝您的回答。 我已經使用你的想法,但它發生在與bevorher相同的現象。 – partykeller1408
可以在gameLoop.join()凍結之前打印gameLoop.running的值嗎?它的價值是什麼?你也可以在遊戲線程循環中打印它嗎? –
gameLoop.runnig的值在endgame()true開始。 – partykeller1408