我目前正在尝试创建一个Clicker / Money游戏,需要一些帮助。因此,基本上,该应用程序包含一个按钮,每次单击该按钮都会提供+1个硬币。如果您以购买“10个硬币加倍硬币”为例,我想将金额从+1更改为+2。
Click here to see how the app looks right now, it might be easier to understand.
下面是一些可能相关的编码。
@IBAction func button(_ sender: UIButton) {
score += 1
label.text = "Coins: \(score)"
errorLabel.text = ""
func doublee(sender:UIButton) {
score += 2
}
@IBAction func points(_ sender: UIButton) {
if score >= 10 {
score -= 10
label.text = "Coins: \(score)"
doublePoints.isEnabled = false
xLabel.text = "2X"
xLabel.textColor = UIColor.blue
} else {
errorLabel.text = "ERROR, NOT ENOUGH MONEY"
}
请记住,我刚刚开始编程,将不胜感激所有反馈。谢谢!
最佳答案
添加一个变量,该变量跟踪单击按钮时获得的积分,以及购买得分乘数时,相应地增加变量,如下所示:
var scoreIncrease = 1 // this is how much the score increases when you tap
// This is called when the "CLICK HERE" button is tapped
@IBAction func button(_ sender: UIButton) {
score += scoreIncrease
label.text = "Coins: \(score)"
errorLabel.text = ""
}
// This is called when you buy the 2x
@IBAction func points(_ sender: UIButton) {
if score >= 10 {
score -= 10
label.text = "Coins: \(score)"
doublePoints.isEnabled = false
xLabel.text = "2X"
xLabel.textColor = UIColor.blue
scoreIncrease *= 2 // increase by x2
} else {
errorLabel.text = "ERROR, NOT ENOUGH MONEY"
}
}
关于ios - Xcode UIbutton函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47913596/