从两个单独的SKScene中检索并立即更新高分

时间:2023-01-23 07:30:01

In my GameScene I have a score that is being saved at the end of the game via UserDefaults. Below is my GameScene code for saving the score.

在我的GameScene中,我得到的分数是在游戏结束时通过UserDefaults保存的。以下是用于保存分数的GameScene代码。

var totalPoints = 0 {
        didSet {
            score.text = " \(totalPoints)"
            UserDefaults.standard.set(totalPoints, forKey: "scoreDefaults")
        }
    }

Once the player loses all their lives, the game then returns you back to the Main Menu scene. In this scene I would like to instantly retrieve and update the high score number on the Main Menu scene if the players last score was higher than the higher score they had before. But for some reason it just constantly updates every score the user gets at the end of the game regardless if it's higher or lower than the last high score. What am I doing wrong? Below is my Main Menu scene. When I print "newScore" and "getScore" I do see the right values but my code must be wrong when it involves updating the high score when needed.

一旦玩家失去了所有生命,游戏就会返回主菜单场景。在这个场景中,我想立即检索并更新主菜单场景中的高分数,如果玩家最后得分高于他们之前的高分。但由于某种原因,它只是不断更新用户在游戏结束时获得的每个分数,无论它是高于还是低于最后一个高分。我究竟做错了什么?下面是我的主菜单场景。当我打印“newScore”和“getScore”时,我确实看到了正确的值,但是当涉及在需要时更新高分时,我的代码必定是错误的。

let getScore:Int = UserDefaults.standard.integer(forKey: "scoreDefaults")

    override func didMove(to view: SKView) {        


    if ((UserDefaults.standard.integer(forKey: "updateHighScoreDefaults")) >= 0) {
                highScore.text = " \(newScore)"
            } else {
                print("I don't know")
            }

            if (getScore > newScore) {
                print("New High Score!")
                newScore = getScore
                //print(newScore)
                UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")
                highScore.text = " \(newScore)"
            } else {
                print("Not a high score")
                UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")
                highScore.text = " \(newScore)"
            } 
    }

1 个解决方案

#1


0  

Your keys are wrong scoreDefaults vs updateHighScoreDefaults Pick 1

你的密钥是错误的得分默认与更新高分数默认选择1

Also, getScore is only called once, make it a computed property instead

此外,getScore仅调用一次,而是将其设为计算属性

var getScore:Int {return UserDefaults.standard.integer(forKey: "updateHighScoreDefaults")}

var getScore:Int {return UserDefaults.standard.integer(forKey:“updateHighScoreDefaults”)}

override func didMove(to view: SKView) {        

        if (getScore > newScore) {
            print("New High Score!")
            newScore = getScore
            //print(newScore)
            UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")

            highScore.text = " \(newScore)"
        } else {
            print("Not a high score")
            UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")
            highScore.text = " \(newScore)"
        } 
        UserDefaults.syncronize()
}

#1


0  

Your keys are wrong scoreDefaults vs updateHighScoreDefaults Pick 1

你的密钥是错误的得分默认与更新高分数默认选择1

Also, getScore is only called once, make it a computed property instead

此外,getScore仅调用一次,而是将其设为计算属性

var getScore:Int {return UserDefaults.standard.integer(forKey: "updateHighScoreDefaults")}

var getScore:Int {return UserDefaults.standard.integer(forKey:“updateHighScoreDefaults”)}

override func didMove(to view: SKView) {        

        if (getScore > newScore) {
            print("New High Score!")
            newScore = getScore
            //print(newScore)
            UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")

            highScore.text = " \(newScore)"
        } else {
            print("Not a high score")
            UserDefaults.standard.set(newScore, forKey: "updateHighScoreDefaults")
            highScore.text = " \(newScore)"
        } 
        UserDefaults.syncronize()
}