gpt4 book ai didi

ios - Xcode UI按钮函数

转载 作者:行者123 更新时间:2023-11-29 11:38:25 26 4
gpt4 key购买 nike

我目前正在尝试创建答题器/金钱游戏,需要一些帮助。所以基本上,该应用程序由一个按钮组成,每次单击它时都会提供 +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 UI按钮函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47913596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com