我正在開發一個遊戲。如果用戶在玩遊戲線程時按下設備後退按鈕暫停,並且屏幕上顯示一個警告對話框。如果用戶不想退出遊戲線程應該恢復並且提示對話框應該消失..但問題是我可以恢復遊戲線程但不能取消提示對話框。以下是我的後退按鈕事件代碼片段。警報對話框沒有被取消
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MainGamePanel.thread.setRunning(false);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Exit Alert");
// alertDialog.setIcon(R.drawable.appicon);
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) {
if(mainscreenOn)
{
dialog.cancel();
}
else
{
dialog.cancel();
GamePanel.thread.setRunning(true);
GamePanel.thread.run();
}
return;
}});
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
如果mainscreenOn標誌爲真,則警報對話框越來越cancelled..but問題就來了,如果它是false.can人幫助我,請我被困在這......
我才知道,這不是警告對話框的問題......這是我的比賽thread..given下面的問題是我的遊戲線程代碼..
public class GameThread extends Thread {
private static final String TAG = GameThread.class.getSimpleName();
// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private MainGamePanel gamePanel;
// flag to hold game state
public boolean running;
public void setRunning(boolean running) {
this.running = running;
}
public GameThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
Log.v(""," ->Inside Thread!" + running);
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
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
}
}
}
當我調試控制是怎麼回事代碼到此代碼並卡在這裏..
我知道這是從這裏調用線程的問題..但我不知道如何修復它... – 2011-12-22 06:30:16
我以爲你不知道這個原因,對不起,我無法幫助你解決你的問題。再見 – 2011-12-22 06:32:30
如果我沒有調用運行()方法,那麼我怎麼能從這個調用?我已經嘗試過只有setRunning(true)語句。但它不工作.. – 2011-12-22 07:52:04