我有一個功能,遊戲(),它看起來像這樣:Android的處理器運行的異步
public void gamePlay()
{
for (int i = 0; i < 4; i++)
{
currentRound = i;
if (i == 0)
{
simulateTurns();
}
if(i == 1)
{
simulateTurns();
}
if (i > 1)
{
simulateTurns();
}
}
}
simulateTurns()使用與Looper.getMainLooper()的參數實例化的處理程序如下之前的遊戲()是曾經被稱爲:
thread = new Handler(Looper.getMainLooper());
simulateTurns():
public void simulateTurns()
{
currentPlayer = players[playerIndex % numPlayers];
if(currentPlayer.getPlayerID() == USER_ID) //user
{
if(!currentPlayer.hasFolded())
showUserOptions(currentRound);
else
{
playerIndex++;
simulateTurns();
}
}
else
{
hideUserOptions(0);
// Give players the option to raise, call, fold, checking (dumb for now, needs AI and user input)
// currentPlayer.getPlayerID() gets the current ID of the player
int randAction = (int) (Math.random() * 4);
String action = "";
//DEBUG Simulate game
Log.w("--------DEBUG--------", "Round: " + currentRound + " Bot: " + currentPlayer.getPlayerID() + " action: " + action + " pot: " + pot);
thread.postDelayed(new Runnable(){
public void run()
{
if(!betsEqual())
simulateTurns();
}}, 5000);
}
}
在調試日誌,所有輪次看m並行開始,然後記錄第3輪的幾圈。
如何讓for循環與simulateGame()同步運行,以便循環順序運行?
注意:我也在幾個onClickListeners上調用了simulateTurns()。
我發現你的代碼很混亂。也許你應該考慮重新設計它 – nandsito
有一個混亂,但thread.postDelayed也許你的問題。你想要的是每次停止/暫停/睡眠5秒鐘,是不是?如果那樣,改變這個代碼,處理程序不能像這樣工作。 –