Events for Adding and Removing Nodes
Adding or showing hidden nodes at runtime is an important part of any app. You’ll probably find the easiest thing to do is simply move Elements out of the visible area and set their Hidden property to YES. Then when needed, use the Show property and move the Element where you want it. But if that’s too much clutter, you can add an Element from ANY other Scene (.sks) file using the Add property….
- Add – Create a Dictionary named Add, then include the two required properties, FromFile and WithName to identify the .sks file and Element. This will clone the Element (without adding the entire scene as a reference. Optionally you can set the NewName property to rename the clone.
- AddRandomElementFromFile – value should be a .sks file with multiple Element nodes (only one will be chosen to show). These elements get added in their same locations as the sks file.
- AddReferenceFromFile – value should a .sks file. Everything in the SKS file will be created and added.
- Show – value can be blank to show the current node touched or the value can be the name of any Element, SKSpriteNode or SKReferenceNode to show. This changes it’s .hidden property to false. This will affect all nodes with that same name. You can change the String value to Array to add multiple name values.
- ShowOrHide – like the Show property but it will show the node if it is hidden, or hide the node if it is showing.
- Hide – Like the Show property, but it will hide the node.
- Remove – value can be blank to remove the current node (if used as a touch event) or the value(s) can be the name of any node remove.
Technically this changes it’s name property to “removeNode“, then after physics are simulated that frame, anything with a .name property of “removeNode” is cleared out. This will also change the node’s physics body Category Mask to 0 so it no longer responses to interactions before it is removed
If you stumbled onto this article, it is part of our documentation for the Story Tellers iOS Starter Kit 2. The kit enables you to make children’s book apps and games without writing any code! But it is Swift 2 based and compatible with iOS9 (or higher) and Xcode 7 (or higher), so kit buyers can even extend the functionality to fit their needs further. Some of what we cover in the kit documentation may apply to Xcode in general, so this article could be worth a read even if you aren’t a user. You can purchase Lifetime Updates the kit here, or subscribe Yearly to CartoonSmart and get the latest version, plus access to all of our other kits / tutorials.
We’ve also created an iBook to document the very latest properties in the kit, so be sure to download that as well.