3

I have asked this similar question a while ago, but since then it seems SKNode now has this nifty initializer init?(fileNamed filename: String) that unarchives the scene from an .sks file. The thing is, this initializer is on SKNode, not SKScene, so I am trying (mostly out of programmer curiosity) to use it on a node, any node, that is not SKScene or a subclass of it.

Now I know I can put the node I want as a child of the .sks file, load the scene using SKNode(fileNamed:... and then pull out the node using childNode(withName:.... But since the fileNamed initializer is declared for SKNode, you should be able to directly load an SKNode and cut out the SKScene middle man. If this is not possible, then why wouldn't the initializer be declared in SKScene?

So, I have tried this, but it appears the root node for the .sks file is strongly suggested to be a descendant of SKScene as seen in this dialog box:

enter image description here

Ignoring caution, I made an .sks file that looks like this:

enter image description here

I made the root Scene be a custom class of an SKNode subclass, and I got this build error:

[MyProject.SolarSystemNode setBlendMode:]: unrecognized selector sent to instance

This is because the blendMode property is declared on SKEffectNode. The class inheritance goes SKNode -> SKEffectNode -> SKScene. So apparently the fileNamed: initializer, though declared on SKNode calls a property on SKEffectNode.

So, is it possible to load an SKNode directly, or am I stuck arranging my node tree like this:

enter image description here

and loading the scene first then pulling out the solarSystemNode? If it is possible to load an SKNode directly from an .sks file with the fileNamed initializer, then could someone please show me an example how, preferably in Swift 3? If it is not possible, could someone please tell me why the fileNamed initializer would be declared on SKNode and not SKScene?

Community
  • 1
  • 1
mogelbuster
  • 1,066
  • 9
  • 19
  • 1
    too drunk to give a proper answer, but isn't this what `SKReferenceNode` is for? https://developer.apple.com/reference/spritekit/skreferencenode – Fluidity Mar 16 '17 at 02:57
  • @Fluidity Thank you! I have been meaning to delve into `SKReferenceNode`. I am having a hard time finding documentation or a tutorial that shows you how to set it up in the scene editor **AND** how to properly load it from code. Most show it exclusively in the editor, and the few that show it programmatically, don't show how it was set up in the editor. When they show one, it leaves me wondering exactly how they set up the other. If you find yourself sober with some free time, could you give me a demo, or point me to a complete tutorial? Either way, thanks for the heads up! – mogelbuster Mar 25 '17 at 21:47
  • @Fluidity I should mention, I starting playing around with it, and caused xcode to crash and refuse to reopen even after I rebooted. I had to delete the sprite kit scene file before Xcode would open my project. I think I caused the reference node to reference its containing scene. Guess Xcode doesn't like infinite loops! – mogelbuster Mar 25 '17 at 21:53
  • i will write something here soon. msg me back in a day if i forget xD – Fluidity Mar 26 '17 at 21:32

0 Answers0