Previously I was questioning if i could use the Scene editor to create complex SKSpriteNodes vs just using it to layout SKScenes. With the help of @Anton Rogachevskyi the previous question I started using unarchiveNodeFromFile to locate the .SKS file. It loads the sks file as I would expect, but when I try to add it to the current scene nothing appears. If I break on the addChild line and preview the object it displays in the preview window properly, so I know it is finding the correct file. Inside the sks file is a custom dialog class and setting breakpoints in "required init?(coder aDecoder: NSCoder)" I can tell that the custom object is getting initialized properly.
inside the dialog class I programmatically add a pink box to the object to show that it does reach the init of the class
class TeamDialog: SKSpriteNode {
init(size: CGSize) {
super.init(texture: nil, color: .red, size: size)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
let pinkBox = SKSpriteNode(color: .magenta, size: CGSize(width: 100, height: 100))
pinkBox.zPosition = 500
addChild(pinkBox)
}
}
And in the Scene this is how I find the object and try to add it to the scene, it appears in the preview but nothing shows up on the screen
private func displayDialog() {
if let wtf = unarchiveNodeFromFile(file: "TeamDialog")! as SKNode? {
let layer = SKSpriteNode(color: .clear, size: self.size)
layer.zPosition = 5000
layer.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
addChild(layer)
for child in wtf.children {
print("child (child.name)")
if let teamDialog = child as? TeamDialog {
teamDialog.removeFromParent()
layer.zPosition = 1
layer.addChild(teamDialog)
}
}
}
}
func unarchiveNodeFromFile(file: String) -> SKNode? {
if let path = Bundle.main.path(forResource: file, ofType: "sks") {
let fileData = NSData.init(contentsOfFile: path)
let archiver = NSKeyedUnarchiver.init(forReadingWith: fileData as Data!)
archiver.setClass(SKNode.classForKeyedUnarchiver(), forClassName: "SKScene")
let node = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode
archiver.finishDecoding()
return node
} else {
return nil
}
}
Again, I can see it in the preview window but nothing appears when it is added to the scene.
Do I have to handle an object that has been unarchived differently or do something to it before I can add it to the scene?
I pulled the code out and have put it in a test project with the same results.
I've tried adding the sks file (as a node) to the scene with no luck, and i've tried isolating out the dialog from the sks file and adding it with no luck
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…