During the WWDC "What's new in SpriteKit" presentation they talked about SKReferenceNode. I can't find any info how to use this, not in the docs, sample apps or release notes. Does anyone have any idea how SKReferenceNode is supposed to work?
SKReferenceNode missing in action
Do you have Xcode 7.0 beta installed ?
You should find SKReferenceNode in the new documentation. Check your downloads in Xcode preferences.
I found the API docs, but there is no clear example (anywhere yet) how to actually use it.
An SKReferenceNode references another scene file and loads all the contents of that scene into the current scene as an SKNode. You can position the reference node normally, including making it a child of another node (like a camera node). One example would be to create an .sks file and add overlays like score/level labels that you want in all your scenes/levels. Then you add a reference node in all your scenes that you want to include this content.
Basically do this
- Create a new scene file with the contents you want to load into another scene (labels, etc.)
- Add contents to it using the scene editor.
- Open the scene file you want to add the reference to in the scene editor.
- Add a reference node to your scene by dragging the 'Reference' icon (dashed square) into the scene.
- Set the refrence nodes parent, position, etc. as usual.
- Make sure you set the 'Reference' field (right under the 'Parent' field) in the property editor for the reference node to the name of the .sks file you want to use as a reference. It's easy to forget this part.
As far as I can tell this provides a handful of advantages:
- Organizing your scenes by breaking them up into sub-scenes.
- Re-using scene components like score/level indicators.
- Allowing you to change one scene component and have it updated in all the scenes that use it.
Thank you for the in depth reply.
Are Reference nodes also a usefull way to seperate character animations? For example a player character with all animations in one file?
I don't think so, I think it's more for visible scene elements you want to use in many scenes (like backgrounds or overlays). The key being that these elements will show up when you add the reference.
I think a better choice for character animations would be to create a SpriteKit Action file. You can do this by selecting New File->iOS Resource->SpriteKit Action. This will create a new file that looks like a scene file and has a timeline for adding actions. You can drag and drop actions like "Animate with Textures" and set the properties as well as give the actions names. Then you can look these actions up using
let action = SKAction(named: "some action")Then run the action on the sprite you want to animate, move, etc. One thing that I didn't realize at first is that you pass in the name that you gave to the action in the Action file, not the name of the file itself. Somehow the action files seem to get searched when you use the SKAction(named: String) initializer. You don't have to explicitly load them.
Thanks!
Still, did you try to actually "use" this Reference Node when the SKS it uses has a custom class?! I can't figure out how to do this. Imagine the example of your score Reference. How would you cann the method "updateScore" from within your "caller" scene?