我以编程方式创建我的精灵工具包游戏,而sks文件最终让我感到困惑。
有没有办法在不使用sks / scene文件的情况下加载或创建切片贴图节点?
答案 0 :(得分:1)
我找到了一种方法:
3)添加此扩展程序以加载当前场景中的场景文件:
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = Bundle.main.path(forResource: file as String, ofType: "sks") {
let sceneData = NSData(contentsOfFile: path)
let archiver = NSKeyedUnarchiver(forReadingWith: sceneData as! Data)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
然后从sks文件中获取tile图。奇怪的诀窍是你必须先从它的父母中删除它:
guard
let tileScene = SKScene.unarchiveFromFile(file: "TileMaps"),
let testMap = tileScene.childNode(withName: "Dungeon1")
as? SKTileMapNode else {
fatalError("Background node not loaded")
}
self.testMap = testMap
self.testMap.removeFromParent()
self.testMap.zPosition = 1200
self.addChild(testMap)