2011-12-22 90 views
0

我正在開發一個遊戲。如果用戶在玩遊戲線程時按下設備後退按鈕暫停,並且屏幕上顯示一個警告對話框。如果用戶不想退出遊戲線程應該恢復並且提示對話框應該消失..但問題是我可以恢復遊戲線程但不能取消提示對話框。以下是我的後退按鈕事件代碼片段。警報對話框沒有被取消

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 
    } 
} 



} 

當我調試控制是怎麼回事代碼到此代碼並卡在這裏..

回答

0

我測試你的代碼,而的GamePanel報表,其做工精細兩個mainscreenOn等於真實,FLASE

我覺得你的情況它不是從onClick()返回時mainScreenOn是假,意味着其在GamePanel.thread.setRunning()GanePanel.thread.run() strucking。


選擇此鏈接可能對您有幫助。關於線程的暫停和恢復。

How to pause/resume thread in Android


要調用setRunning(true)run()。我認爲如果你的線程有Runnable對象,那麼只有你必須調用run()。如果您不使用Runnable對象,如下

Thread t = new Thread(new Runnable() { 
     public void run() { 
      // your task 
     } 
}); 

,我認爲沒有必要要求run()。無論如何,你正在用running變量來控制線程。

請檢查這一點,我也對此沒有信心。

+0

我知道這是從這裏調用線程的問題..但我不知道如何修復它... – 2011-12-22 06:30:16

+0

我以爲你不知道這個原因,對不起,我無法幫助你解決你的問題。再見 – 2011-12-22 06:32:30

+0

如果我沒有調用運行()方法,那麼我怎麼能從這個調用?我已經嘗試過只有setRunning(true)語句。但它不工作.. – 2011-12-22 07:52:04

0

你需要調用dismiss()對話框而不是cancel()。

dialog.dismiss(); 

試試這個

+0

不能正常工作.... – 2011-12-22 06:29:16

0

對於每個條件你取消dialog.So我想你不應該檢查 cancelling.You可以像下面。

alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         if(!mainscreenOn) 
         { 
          GamePanel.thread.setRunning(true); 
          GamePanel.thread.run(); 
         } 


        return; 
       }}); 

編輯:

然後用肯定按鈕嘗試和負Button.If它不工作,然後檢查這是由「Yugandhar巴布」中提到的點。

AlertDialog.Builder builder = new AlertDialog.Builder(this);; 
       builder.setMessage("Are you sure you want to exit?") 
         .setCancelable(false) 
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           finish(); 
          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
+0

我試過這個..但沒有用...仍然沒有取消提醒對話框 – 2011-12-22 06:29:03