swift - 如何在 Spritekit 和 Swift 5 中保存分数
问题描述
我查看了整个互联网,以了解如何使用 NSUserDefaults 保存分数。他们都没有最终工作。可能它们都来自 Swift 3。我希望弄清楚如何在 Swift 5 和 Spritekit 中做到这一点。我正在制作一款名为 Plane Dash 的 Flappy Bird 游戏,我想保存分数并最终添加高分功能。
到目前为止 我已经尝试过:到目前为止我尝试过的代码。
解决方案
您可以使用 UserDefualts。例如
var score = 0
//SET
UserDefaults.standard.set(score, forKey: "score")
//RETRIVE
score = UserDefaults.standard.integer(forKey: "score")
编辑:
var currentScore = 100 //change that to the players current score.
let highScore = UserDefaults.standard.integer(forKey: "highScore") //Get the users high score from last time.
if(currentScore > highScore){// check and see if currentScore is greater than highScore.
UserDefaults.standard.set(currentScore, forKey: "highScore")//if currentScore is greater than highScore, set it in UserDefualts.
}
编辑 Pt.2
labelName.text! = "\(highScore)"
推荐阅读
- javascript - @angular/pwa 服务人员不在我已经存在的项目上工作
- wordpress - 如何在 WooCommerce 中识别输入字段的元键
- sql - oracle如何将分钟转换为天
- python - 从 json 文档的两个节点中提取带有 JSONPath2 的属性子集
- r - 根据 R 中的数据框列表创建 ts 对象列表
- mysql - 为什么 MySQL 会为同一个用户创建 4 个连接?
- python - 如何比较两个子类是否具有相同的超类?
- python - 在另一个列表理解中过滤列表理解
- c# - 为什么我无法将 IntPtr 转换为 byte[]?
- java - Swift 和 Android 之间的 UI 并行性