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

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

问题描述

我正在尝试保存一个highScore"整数,该整数在使用 SpriteKit 关闭应用程序后仍然存在.

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 单例,你可以定义一个计算变量来保存和读取 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天全站免登陆