Swift 3.0使用SpriteKit保存得分 [英] Swift 3.0 save a score with SpriteKit

查看:80
本文介绍了Swift 3.0使用SpriteKit保存得分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存使用SpriteKit关闭应用后持续存在的highScore整数。

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.

我知道有一个很多选项已经在stackoverflow上,但是它们都适用于小于3.0的Swift版本。他们使用了一堆我不懂初学者的方法和内置功能。在尝试将所有这些选项转换为Swift 3.0并失败后,我希望有人可以向我展示一些示例代码如何执行此操作。

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.

推荐答案

您可以为此目的使用 UserDefaults 类。

You can use UserDefaults class for that purpose.

如果您有任何 GameManager 单例,您可以定义一个计算变量,该变量可以保存并从<$读取c $ c> UserDefaults :

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")
    }
}

这篇关于Swift 3.0使用SpriteKit保存得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆