2011-12-21 66 views
0

我正在开发一个game.while玩游戏,如果用户按下设备后退按钮,我必须暂停线程并显示一个警告框,它确认用户是否真的想退出或不。如果用户想要退出它只需退出游戏。但是当用户不想退出时出现问题,那么我必须恢复线程。但我不能这样做..下面是我的代码片段..如何暂停和恢复我的应用程序中的线程?

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

更新()和渲染是写入MyGamePanel类两种功能。

我已经尝试了许多事情,但没有一个working..please帮我...

+0

请帮我...我卡在这.. – 2011-12-21 09:22:17

回答

1

通常哟不想使用resumesuspend,因为他们可以处于不一致的状态离开线程。做你正在尝试做的最好的办法是用旗语:

  • 声明一个布尔的地方,您可以指定,如果游戏暂停:

    Boolean paused; 
    
  • 检查这个布尔在游戏循环和等待,如果暂停:

    synchronized(paused) { 
        while(paused) { 
         wait(); 
        } 
    } 
    
  • 设置和取消暂停布尔来自主线:

    public void setPaused(boolean wantToPause) { 
        synchronized(paused) { 
         paused = wantToPause; 
         notify(); 
        } 
    } 
    

这是原理图代码,它例如不处理异常,但您明白了。

+0

其实我很困惑,我必须写这个代码...同步()函数在thread..rt?笏abt另一个?我怎么能控制这在后面的关键事件?对不起..我不熟悉线程.. :( – 2011-12-21 09:03:28

+0

我试图编写同步功能在我的线程..但它是犯错误..我不知道在哪里我必须写代码片段.. – 2011-12-21 09:24:14

+0

'synchronized(){}'结构是一个特殊的Java结构,不是函数调用,你应该在你的游戏循环中使用它(在'while(running){...}')中。执行到达该点后,线程将停止并等待,直到“paused”为false。 – gimix 2011-12-21 10:49:46

-1

我有同样的问题,但我发现解决方案只是在线程中替换此代码。

while (true) { 
if(running){ 
Log.d("running", "true"); 
    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); 
     }`enter code here` 
    } // end finally 
} 

}

+0

只需在线程中替换此代码并跳过对话框中的这一行代码即可。 //GamePanel.thread.resume(); – 2015-07-26 16:11:21

0

我觉得你的问题是在你的MyGamePanel.thread.resume();。尝试删除该代码,看看会发生什么。

如果你认为thread.resume()是你为了不同目的需要的函数(你被限制删除它),那么向我们展示thread.resume()函数代码,以便我们可以尝试帮助你修复恢复功能代码。