我有一个功能,游戏(),它看起来像这样: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秒钟,是不是?如果那样,改变这个代码,处理程序不能像这样工作。 –