Learn iOS with Examples, Code Snippets and Video Tutorials

SpriteKit’s insertChild, addChild and zPosition

SpriteKit’s insertChild, addChild and zPosition
October 3, 2013 Justin

I’m about 2 weeks into using Sprite Kit and I’ve finally come face to face with a reason to use insertChild. Quickly, my project involves a world node which contains everything, but in particular a few child nodes which are knight characters roaming around. Basically the world is setup something like this inside of my Level class …

Then each child is added like this…

CSCharacter is just a class subclassed from an SKNode, (which will be a container for things like a health meter, the actual character artwork, particle FX during an attack, etc). I’m adding in three characters, and initially I don’t really care about their zPosition, but I could set it like so…

This would put that particular character instance at a  depth of 100 , so visually above any nodes inside of myWorld that are at a depth of 0 to 99.  By default the characters are all going in at 0, and the last one added will be higher than the previous one. For those playing the comparison game of “Cocos2d vs Sprite Kit”, this was the same with Cocos2d too.  And the nice thing about both languages is you did not have to worry about keeping the zPosition (or zDepth) within any kind of range of the number of actual children you had in the scene. So it was harmless to put an object at 100 or 1000 even if you only had a few children in the scene. Anyone familiar with Actionscript 3’s awful “the supplied index is out of bounds” error will appreciate this flexibility.

Sprite Kit does have the option to insert a child at a particular index, but this is not the zPosition. If I had added instances named charactercharacter2 and character3 in that order, and later on I wanted character3 to be evaluated (or enumerated) before the other children, I could write…

Let’s take a look at that enumerate code real quick…

This would run through every child inside of myWorld with the .name property of  @”character”.  Alternatively, I could have used @”*” to find every child. Or I could have written @”//character”,  which would have checked for children inside of other children with that name. In this case, both options are overkill. 

So the child that I used the  insertChild: method on, would get evaluated first in the enumerate block. Assuming of course, I inserted the child at the 0 index.

And why you might ask, would you ever need to do that??  Well, put it this way. You’ll know it when you see it. In my case, I had one knight acting as the leader, and the other knights were following that character around.  I made some assumptions early on in my code, that the first character child enumerated was always the leader. So when I later added code to swap leaders, a quick fix was to insert the new leader at the 0 index.

That’s all folks. If you want to learn more Sprite Kit, be sure to check out the video tutorials I’m teaching at CartoonSmart.com in the Code section.  A free three hour Sprite Kit tutorial is already available.

Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast


Leave a reply

Your email address will not be published. Required fields are marked *


Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com