Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Child Basics in Sprite Kit – Adding, Removing, Finding

Basic Parent-Child Concepts in Sprite Kit

One important aspect to understand early on when working with Sprite Kit is that everything is a child of something. When working off the Sprite Kit template, we start off with a scene (the GameScene), and begin adding to it from there. You could add children in the Scene Editor, by selecting your GameScene.sks file

The Game Scene SKS file adding children

And you’ll notice there already is one child: an SKLabelNode. Most likely you want to delete that. Instead add a Color Sprite (which is technically an SKSpriteNode).

Drag that into the scene. And give it a texture and name. If you don’t have any images in your Assets Catalog (Assets.xcassets) thats okay, Sprite Kit always includes a spaceship by default. Set the Texture to Spaceship and the Name to Ship.

How to Name and Texture Images in Sprite Kit

You’ll notice the Parent is already set to SKScene_0 by default. If we had more than one sprite in the scene, we could parent our spaceship to something else. If we did that, then moving around the parent would also move the shapeship.


Adding a Child to the Scene with just Swift / Sprite Kit Code

Let’s step away from the Scene Editor for now and do the exact same thing but with code. In your GameScene.swift file, make the entire file look like this….

Pretty easy right, its really just three lines of code that added the child. Actually two could have done it. I only added ship.name = “Ship” so what we did here is identical to what we did in the Scene Editor.

Notice that when adding the child I wrote…

Focus on the self part. Self refers to the SKScene here. I could have replaced self with the instance name of another child in the scene. For example…

In which case, ship would have been a child of blockadeRunner and moving / transforming blockadeRunner would affect the ship.


Finding Children in a Scene

Often you need to identify children by their names in Sprite Kit. Now keep in mind in both examples above, where we added the child in the Scene Editor and simply with code, we didn’t declare any kind of persistent reference variable to the ship. Even though our code has an SKSpriteNode variable named ship, this variable only exists while our didMove function is run. Easy come, easy go.

To see how to declare a variable and make it reference a child in the scene, read our article on Casting in Sprite Kit  (for important game elements, like the player, that’s always recommended)

For the purposes of this lesson though, we’ll only rely on the .name property to find our child. Sprite Kit has this very handy locator   .childNode(withName: ) . See it in action…

Notice I’ve stepped out of the didMove function, so our ship variable no longer exists. We couldn’t write ship inside findTheChild() without getting an error (ship would be considered out of scope).

Obviously we aren’t doing much once we find the child in this example, other than printing out a statement to the Output Window, but nonetheless it proves we can locate the child by its .name property.


Searching for Children with a For Loop in Sprite Kit

An important aspect of Sprite Kit is that your scene can have many nodes with the same name. For example, you might spawn 100 enemies each with the name “Enemy”. Thats perfectly okay!  It actually keeps things simple. So at times, you aren’t looking for one child with a particular name, you’re looking for many. Running self.childNode(withName: ) like we did in the previous example, would only look for one child.  Once it found the first node, it would successfully print “wasn’t nil, we found it”, and it would NOT look for more.

So lets try the same thing again with a for loop. Add this inside the findTheChild function…

So in this case, we’re iterating through every child node in the Scene (sounds daunting, but its really not), and if the node’s name property equals “Ship” we print out that we found one.

Of course without multiple nodes in the scene named “Ship” , you’ll only see one statement in the Output Window. Feel free to add more Sprites to the SKS file to test this.

So what’s a more practical example for this. You could track the number of enemies currently in this scene, like so…


Searching for Children inside Children in Sprite Kit

You can also enumerate through all children’s children in the scene using this…

If you needed break out of this (stop enumerating), you would write…


Searching Within All Children for Partial Name Matches

Lets get even crazier and add our ship within a parent, then call it “ShipBlue” and look for children that contain “Blue” in their name. Here goes…


Removing a Child or Children

This is easy. In most of our examples above if we wanted to simply remove one of the nodes we could have written…

Or…

The ? mark just means this would fail gracefully (without crashing) if that child wasn’t found.

And occasionally you might even want to remove all the children from a parent.


One last thing…

One neat aspect of iterating through all the nodes in your scene is you can do all sorts of things to them. Every node can have SKActions run on them, and since all children are subclasses from SKNode, you can access all the basic SKNode properties.

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com