我正在开发一个游戏。在玩游戏时,如果用户按下设备后退按钮,我必须暂停线程并显示一个警告框,确认用户是否真的想退出。如果用户想退出,只需退出游戏。但是当用户不想退出时出现问题。然后我必须恢复线程。但我不能这样做..下面是我的代码片段..
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MyGamePanel.thread.setRunning(false);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Exit Alert");
alertDialog.setMessage("Do you really want to exit the Game?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
return;
} });
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MyGamePanel.thread.resume();
MyGamePanel.thread.setRunning(true);
return;
}});
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
下面给出的是螺纹部分。
@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
update() 和 render 是 MyGamePanel 类中编写的两个函数。
我已经尝试了很多东西,但没有一个有效..请帮助我...