gpt4 book ai didi

swift - Spritekit 场景归档

转载 作者:行者123 更新时间:2023-11-28 06:27:38 25 4
gpt4 key购买 nike

SpriteKit programming guide包含这个有用的小花絮:

Store a game level as an archive of a scene node. This archive includes the scene, all of its descendants in the node tree, and all of their connected physics bodies, joints, and actions.

但它没有任何关于在哪里或如何存储这个文件的信息。这是我当前的代码,但它不起作用:

    // Save Data
let fileManager = FileManager.default
let URL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
let path = URL.appendingPathComponent("stage1.dat")

if NSKeyedArchiver.archiveRootObject(stage1, toFile: path.path) != false {
print("stage saved")
}
else {
print("save failed")
}

// Load Data
var data = Data()
do{
data = try Data(contentsOf: path)
}
catch{
print(error)
}


let stage = NSKeyedUnarchiver.unarchiveObject(with: data) as? GameScene

编辑:原来我没有写入本地域的权限。哎呀。我将其切换到用户域,现在写入工作正常。但是,尝试取消归档同一个文件现在会抛出 NSException。我将取消存档代码添加到上面的 block 中。

最佳答案

我最终使用 unarchiveTopLevelObjectWithData 来生成一个我可以捕获的错误。

原来 NSexception 是由于尝试使用不是解码对象的东西来解码可选的。

This question pretty much sums it up.

关于swift - Spritekit 场景归档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41387184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com