Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Using the update method of your SKScene to update other nodes.

Using the update method of your SKScene to update other nodes.
October 8, 2013 Justin

Your SKScene class, whatever that may be called, has an update method like so…

You’ve already seen that code above if you started your project with the Sprite Kit template (probably you did). Well unlike  Cocos2d, you can’t just take that same update: method and run it automatically in one of your other SKNodes (in Cocos2d you could do that with CCNodes).  This update: method is specific to the SKScene class. Here’s what the official documentation says “Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused.”    Alright, cool. It’s run automatically.

So what about other nodes that need to do something once per frame as well. We can write our own update method in our SKNodes which the SKScene update calls for us. For this example, lets assume we have a class called CSCharacter. In the header file, CSCharacter.h, we would declare…

Then in the implementation file, CSCharacter.m , we need to write that same method…

Excellent. Lets suppose one more thing about this CSCharacter class. At some point (possibly in the init statement), we named it like so…

This gives us a quick way of finding the node (or any node) with that name. So let’s head back over to our SKScene class and look at the code that could call the update method on that instance of CSCharacter (this assumes we’ve added that instance  to the scene already).

So the update method is now enumerating through every child in the scene and if it finds one with the name of @”character” then it will run that block of code with a node parameter passed in.  That node is then cast as our CSCharacter class with an instance name of character. And wha-lah, we just called the update method of the character instance. Notice too that everything is run if self is not paused. Meaning the SKScene is not paused.

Even though the official docs imply the update method is ignored if the scene is paused, I found that wasn’t the case. At least at the time I’m writing this. Maybe that will get patched up eventually, but for now, its easy to just include a simple if statement.

So now if the scene is updating, the child nodes named @”character” are also updating. If the CSCharacter update method was used to move the character around, then obviously the character would stop moving while the main scene was paused.

Pretty cool, eh.

Note: Apologies if some of the coloring doesn’t match. This post was copied over from my not-so-self-hosted blog.

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

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com