gpt4 book ai didi

ios - 如何覆盖 swift3 中的高分

转载 作者:行者123 更新时间:2023-11-30 12:45:40 24 4
gpt4 key购买 nike

下面的代码计算游戏的 react 时间。总 react 时间以totalTime 计算。现在,代码节省了 react 时间。然而,totalTime 始终等于高分。所以不保存高分,只保存当前分数。我希望代码保存最低的 totalTime(如果它小于当前最低的 totalTime)。

import UIKit
class winViewController: UIViewController {
@IBOutlet var score2: UILabel!
@IBOutlet var winningLabel: UILabel!
@IBOutlet var foxMill: UILabel!
@IBOutlet var hhighscore: UILabel!

public var LebelText: String?
public var LebelText2: String?
public var LebelText3: String?
public var LebelText4: String?

override func viewDidLoad() {
super.viewDidLoad()
timeCalculation()
loadState()
}

func saveScore(score: Double) {
// Instantiate user defaults
let userDefaults:UserDefaults = UserDefaults.standard

// Set your score
userDefaults.set(score, forKey: "highScore")

// Sync user defaults
userDefaults.synchronize()
}

func loadState() {
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
foxMill.text = "High Score: \(score)"
}

func timeCalculation(){
guard let unwrapedText = self.LebelText2 else { return }
guard let unwrapedText2 = self.LebelText else { return }
guard let unwrapedText3 = self.LebelText3 else { return }
guard let unwrapedText4 = self.LebelText4 else { return }

if let myInt = Double(unwrapedText), let myInt2 = Double(unwrapedText2), let myInt3 = Double(unwrapedText3), let myInt4 = Double(unwrapedText4) {
var totalTime = myInt + myInt2 + myInt3 + myInt4
self.winningLabel.text = "You won"+"\n"+"Reaction time :" + String(totalTime) + " Seconds"

guard let highScore = UserDefaults.standard.value("highScore") as? Double else { return }

if totalTime > highScore {
saveScore(score: totalTime)
}
}
}
}

最佳答案

您首先在 View 中调用 timeCalculation 确实加载了它,我想它会计算您的“当前高分”,并且您将其保存到用户默认值中。但在保存之前,您不会检查它是否真的是高分。您需要在那里添加条件检查。

guard let highScore = UserDefaults.standard.value(forKey: "highScore") as? Double
else{return}
if totalTime>highScore{
saveScore(score: totalTime)
}

关于ios - 如何覆盖 swift3 中的高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41668824/

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