Swift 3.0 save a score with SpriteKit

﹥>﹥吖頭↗ 提交于 2019-12-20 04:16:30

问题


I am trying to save a "highScore" integer that persists after the app is closed using SpriteKit.

That's it. Just one single integer on one single screen that I will eventually set with my game whether or not the new score is higher than the saved score.

I understand that there are a lot of options already on stackoverflow for this, however they are all for versions of Swift less than 3.0. They use a bunch of methods and built-in functionality that I don't understand as a beginner. After trying to convert all of these options to Swift 3.0 and failing, I am hoping someone can show me some sample code how to do this.

I will be happy to answer any further questions.


回答1:


You can use UserDefaults class for that purpose.

If you have any sort of GameManager singleton, you can define a computed variable which saves and reads from UserDefaults:

var highScore: Int {
    get {
        return UserDefaults.standard.integer(forKey: "highScore")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "highScore")
    }
}


来源:https://stackoverflow.com/questions/40330303/swift-3-0-save-a-score-with-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!