如何在循环中等待按钮按下? [英] How can I wait for a button press in a loop?

查看:190
本文介绍了如何在循环中等待按钮按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个简单的程序,模拟一个棋盘游戏,一些玩家轮流掷骰子移动到董事会。玩家可以是人力或计算机。

如果这是一个命令行风格的游戏,我可以简单地循环播放玩家的diceRoll功能。



如果播放器是电脑播放器,diceRoll只是告诉电脑滚动骰子。

如果玩家是人类,diceRoll将等到用户输入roll命令,然后继续。



如何将这个想法转移到平面设计?我不觉得连续检查看是否按下滚动按钮是有意义的。我正在使用动作脚本2,但想法可以是你想要的任何语言。我只想就最好的设计方式提出一些意见。我不认为有一些我不知道的waitForButtonPress函数?

解决方案

我想我'我找到了一个我喜欢的解决方案。
主游戏类将有一个nextTurn函数,如下所示:

  nextTurn(){
bool guiSet = false
while(guiSet = false){
//获取下一个玩家
如果(下一个玩家是人){
//启用gui(即'Roll骰子按钮)
guiSet = true
} else {
//播放器是一台电脑
//让电脑滚动骰子并作出必要的决定
}
}
}

当一个人类玩家完成轮到时,将打电话给nextTurn继续玩游戏。当电脑完成转弯时,流动仍然在while循环,所以游戏将继续。


Say I have simple program that emulates a board game with a number of players who take turns rolling dice to move across a board. The players can be human or computer.
If this was a command-line style game, I could simply make a loop to iterate over the players which would call the diceRoll function for that player.

If the player is a computer player, diceRoll simply tells the computer to roll the dice.
If the player is a human, the diceRoll will wait until a user inputs the roll command and then continues.

How can I transfer this idea to a graphic design? I don't think it makes sense to continuously check to see if the roll button has been pressed. I am working with actionscript 2, but ideas can be in whatever language you want. I'd just like some opinions on the best way to design this. I don't suppose there's some sort of 'waitForButtonPress' function that I don't know about?

解决方案

I think I've found a solution that I like. The main game class will have a nextTurn function like so:

nextTurn() {
   bool guiSet = false
   while (guiSet = false) {
      //Get the next player
      if (next player is human) {
         //enable the gui (ie. the 'Roll Dice' button)
         guiSet = true
      } else {
         //The player is a computer
         //Have the computer roll the dice and make any necessary decisions
      }
   }
}

When a human player is finished his turn, a call will be made to nextTurn to continue the game play. When a computer finishes a turn, the flow is still in the while loop so the game will continue.

这篇关于如何在循环中等待按钮按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆