gpt4 book ai didi

ios - 如何在单个 View 中每秒将值增加一个数字 (cps)?

转载 作者:行者123 更新时间:2023-11-28 14:03:34 24 4
gpt4 key购买 nike

我一直在查看以前的问题,但是,它们似乎已经过时或不适用于我的情况。在我的应用程序中,我想每秒通过另一个变量 (cps) 增加一个变量 (money)。当我尝试执行永久循环或使用计时器时,我要么出错,要么什么也没有发生。

class SecondViewController: UIViewController {
var money = 0
var cost = 20
var clicks = 1
var cost2 = 50
var CPS: Int = 0
var truth = true

@IBOutlet weak var costOutlet2: UILabel!
@IBOutlet weak var errorLabel: UILabel!
@IBOutlet weak var cpsButtonOutlet: UILabel!
@IBOutlet weak var clicksButtonOutlet: UIButton!
@IBOutlet weak var clicksOutlet: UILabel!
@IBOutlet weak var cpsOutlet: UILabel!
@IBOutlet weak var scoreOutlet2: UILabel!
@IBOutlet weak var costOutlet: UILabel!
@IBOutlet weak var cpsBtnOutlet: UIButton!

override func prepare(for backsegue: UIStoryboardSegue, sender: Any?) {
let controller = backsegue.destination as! ViewController
controller.money = Int(scoreOutlet2.text!)!
controller.clicks = Int(clicksOutlet.text!)!
controller.CPS = Int(cpsOutlet.text!)!
controller.cost = Int(cost)
controller.cost2 = Int(cost2)
}

override func viewDidLoad() {
super.viewDidLoad()
costOutlet2.isHidden = true
costOutlet.isHidden = true
costOutlet.text = "\(cost)"
scoreOutlet2.text = "\(money)"
clicksOutlet.text = "\(clicks)"
cpsOutlet.text = "\(CPS)"
errorLabel.isHidden = true
clicksButtonOutlet.setTitle("$\(cost): +1 Clicks", for: .normal)
cpsBtnOutlet.setTitle("$\(cost2): +1 CPS", for: . normal)
}

override func viewDidAppear(_ animated: Bool) {
scoreOutlet2.text = "\(money)"
clicksOutlet.text = "\(clicks)"
}

@IBAction func addClicks(_ sender: Any) {
if (money >= cost) {
money = money - cost
scoreOutlet2.text = "\(money)"
cost = cost * 3
clicks = clicks + 1
clicksOutlet.text = "\(clicks)"
(sender as AnyObject).setTitle("$\(cost): +1 Clicks", for:[])
print(cost)
} else {
insufficent()
}
}

@IBAction func addCps(_ sender: Any) {
if (money >= cost2) {
money = money - cost2
scoreOutlet2.text = "\(money)"
cost2 = cost2 * 3
CPS = CPS + 1
cpsOutlet.text = "\(CPS)"
(sender as AnyObject).setTitle("$\(cost2): +1 CPS", for:[])
} else {
insufficent()
}
}

func insufficent() {
errorLabel.isHidden = false
errorLabel.text = "Insufficent Funds"
DispatchQueue.main.asyncAfter(deadline: .now() + 3.5) {
self.errorLabel.isHidden = true
}
}
}

我目前正在尝试在 Swift 4 Xcode 10 上制作唱首歌游戏,我可以在购买时增加 CPS,但是,购买时金钱的值(value)并不会每秒都增加。无论“CPS”的值是多少,我怎样才能使变量“money”增加。

最佳答案

尝试使用您的 viewController 中的代码

/...
override func viewDidLoad() {
super.viewDidLoad()
increaseMoneyEverySecond()
}

private func increaseMoneyEverySecond() {
money = money + CPS
DispatchQueue.main.asyncAfter(deadline: .now()+1) { [weak self] in
if let strongSelf = self {
strongSelf.increaseMoneyEverySecond()
}
}
}
/...

关于ios - 如何在单个 View 中每秒将值增加一个数字 (cps)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285140/

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