我的 Android 项目(我的第一个 Android 项目)中的 GameLoop 有问题:
我有一个 GameView (SurfaceView) 启动的 Activity。
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();
它冻结之后。
我已经尝试了很多,但没有任何效果。之前谢谢你的帮助