「你只要懂水晶,水晶就会帮助你。」
回村後,山姆卖出了所有水晶,瞬间成为村子里最富有的人。
其他城镇的宝石商人也闻名而来,想瞧瞧黑森林的样貌。
「想要我的水晶吗?去找吧!全部都在黑森林里!」
从此之後山姆有了另一个名字:「黑森林水晶实价分析师,山大王,山姆」
PS. 这里是开发 iOS 手机游戏的系列文,如果还没看过之前
剧情文章的朋友,欢迎先点这边回顾唷!
延续昨天的显示得分文字,我们继续在游戏画面的右上方加上最高得分的文字节点,一样放在 scoreNode
里面
labelBest
,让他显示的文字为 Best: \(bestScore)
,将标题与分数组合起来显示let labelBest = self.labelBest
fontColor
:设定文字颜色fontSize
:设定文字大小fontName
:设定字体position
:设定位置horizontalAlignmentMode
:设定水平对齐方式verticalAlignmentMode
:设定垂直对齐方式labelBest
加到 scoreNode
里bestScore
class GameScene: SKScene {
...
var labelBest: SKLabelNode?
var bestScore: Int = 0
override func didMove(to view: SKView) {
...
self.labelBest = SKLabelNode(text: "Best: \(bestScore)")
if let scoreNode = self.scoreNode, let labelScore = self.labelScore, let labelBest = self.labelBest {
...
labelBest.fontColor = UIColor.white
labelBest.fontSize = CGFloat(24)
labelBest.fontName = "Copperplate"
labelBest.position = CGPoint(x: self.size.width - 10, y: 0)
labelBest.horizontalAlignmentMode = .right
labelBest.verticalAlignmentMode = .center
scoreNode.addChild(labelBest)
}
}
}
画面右上方显示了最高得分文字
Property list 是一种 XML 格式的档案,可以用来储存一些设定值,使用 key-value
的方式
我们可以发现专案内有一个 Info.plist 档案,它纪录了许多 app 的设定值
为了与原本专案设定做区别,我们额外新增另一个 .plist 档案来储存最高分
请先新增一个 setting.plist
档案
首先点选新增档案
选择 Property List -> Next
将档案命名为 setting,点击 Create 按钮,完成 .plist 档的新增
点击 setting.plist,按下 +
新增一个新的栏位
Key 取名为 bestScore
,Type 设定为 Number
,Value 设定为 0
回到程序码,新增一个结构 (struct),命名为 Setting,内部新增一个参数命名为 bestScore
(与刚刚建立的 setting.plist
内容格式一样)。
让这个结构是 Codable
的类型,即遵循 Decodable 与 Encodable 协定。
struct Setting: Codable {
var bestScore: Int
}
在增加分数时,判断如果目前得分大於最高得分,就将目前得分存进 bestScore
,并且改变 labelBest
的文字显示
class GameScene: SKScene {
...
func addScore(add: Int) {
self.score += add
self.labelScore?.text = "Score: \(self.score)"
if self.score > bestScore {
self.bestScore = self.score
self.labelBest?.text = "Best: \(self.bestScore)"
}
}
}
接着来写储存最高得分的方法,请新增 setBestScore
方法
setting
,让外部带想要储存的资料进来Bundle.main.url
取得 setting.plist
的档案位置PropertyListEncoder().encode
把资料转成 property listclass GameScene: SKScene {
...
func setBestScore(setting: Setting) {
if let path = Bundle.main.url(forResource: "setting", withExtension: "plist") {
do {
let data = try PropertyListEncoder().encode(setting)
try data.write(to: path)
} catch {
print(error)
}
}
}
}
新增好储存方法後,就可以在想要的时机点储存最高分了,我们先设定在怪物与主角碰触时,存入最高分
setting
,bestScore
带入最高得分setBestScore
方法,并且把 setting
带入class GameScene: SKScene {
...
func gameStop() {
...
let setting = Setting(bestScore: self.bestScore)
self.setBestScore(setting: setting)
}
}
有了储存方法後,也需要有取得最高得分纪录的方法,请新增 getBestScore
方法
Bundle.main.path
取得 setting.plist
档案位置FileManager.default.contents
取得档案的内容PropertyListDecoder().decode
将 property list 的内容依照 Setting
结构解析出来self.bestScore
class GameScene: SKScene {
...
func getBestScore() {
if let path = Bundle.main.path(forResource: "setting", ofType: "plist"),
let file = FileManager.default.contents(atPath: path),
let setting = try? PropertyListDecoder().decode(Setting.self, from: file)
{
self.bestScore = setting.bestScore
}
}
}
在游戏一开始时,先取得本机内的最高得分纪录,再接着之前写的新增文字节点,这样就可以显示本机内储存的分数纪录了
class GameScene: SKScene {
...
override func didMove(to view: SKView) {
...
self.getBestScore()
...
}
}
可以看到画面上显示当前的最高得分,并且在重新开启游戏後,还是存有之前的纪录
参考来源:
Information Property List
Codable
encode(_:)
contents(atPath:)
decode(_:from:)
How To: Working with Plist in Swift
<<: DAY 20 『 连接 API 实作 - 天气 APP 』Part2
来到了需求蒐集的第3篇 - 焦点团体访谈。 之前因为专案需求,有机会举办一场焦点团体访谈,常见的焦点...
物件导向 这单元主要是让大家了解物件导向的基本实作 物件导向有三大特性: 封装 继承 多型 封装 类...
所有权可以说是Rust核心概念,这让Rust不需要垃圾回收(garbage collector)就可...
##测试文,透过iT邦帮忙记录学习历程 Console.Write("Hello iT邦帮...
准备出游,6.2 的 Raft 之後再补 5.3 提到 state machine replica...