如何保存当前水平

时间:2018-01-20 16:34:30

标签: swift sprite-kit

我正在制作一个50级以上的游戏。我似乎无法保存游戏,因此当用户稍后打开游戏时,它将处于用户离开的同一级别。我使用fallowing代码从一个级别转换到另一个级别。

let leveltwo = levelTwo(fileNamed: "levelTwo")
leveltwo?.scaleMode = .aspectFill
self.view?.presentScene(leveltwo!, transition:SKTransition.fade(withDuration: 0.1)) 

我还为每个级别都有一个“example.swift”和“example.sks”文件。我的目标是保存用户所在的级别。我没有保存高分,我知道该怎么做。但我不确定它是否相同。或者我如何应用它。

1 个答案:

答案 0 :(得分:2)

您可能想要使用UserDefaults

因此,例如,在您的AppDelegate中,您将实现此目的:

func applicationDidEnterBackground(_ application: UIApplication) {
    UserDefaults.standard.set("level_file_path", forKey: "currentLevel")
}

func applicationDidBecomeActive(_ application: UIApplication) {
    let levelFilePath = UserDefaults.standard.string(forKey: "currentLevel")

    // load your file/level here
}
相关问题