如何解决简单的骰子应用-按时间顺序而不是随机的顺序
我需要在这段代码中进行哪些更改,以使骰子按时间顺序从1变为6(每次我按+1按钮),然后再重复一次,而不是像现在这样随机进行?
解决方法
将以下内容更改为:
**与let diceArray
保持联系! **
class ViewController: UIViewController {
@IBOutlet weak var diceImageView1: UIImageView!
let diceArray = [1,2,3,4,5,6]; /* <- use what you have already
@IBAction func rollButtonPressed(_ sender: UIButton) {
if(typeof(diceImageView1.image)=="undefined") diceImageView1.image=null;
switch(diceImageView1.image) {
case diceArray[0]: diceImageView1.image = diceArray[1]; break;
case diceArray[1]: diceImageView1.image = diceArray[2]; break;
case diceArray[2]: diceImageView1.image = diceArray[3]; break;
case diceArray[3]: diceImageView1.image = diceArray[4]; break;
case diceArray[4]: diceImageView1.image = diceArray[5]; break;
default: diceImageView1.image = diceArray[0];
}
}
}
我们正在检查if(typeof(diceImageView1.image)=="undefined")
,因为我不知道代码的工作原理,因此,请确保设置diceImageView1.image=null;
。
第一次运行时,diceImageView1.image
不应等于任何骰子面,因此default
会将其设置为[0]
,这是骰子面上带有1的骰子。
连续运行将switch
移至下一个:[0]
(1)至[1]
(2)到[4]
(5)至[5]
(6)。 br />
一旦到达[5]
(6),就没有case
,因此如上所述的default
会将其设置为[0]
(1)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。