Learn iOS with Examples, Code Snippets and Video Tutorials

Day 3 – Learn Swift and Sprite Kit – Part 2

Day 3 – Learn Swift and Sprite Kit – Part 2
October 8, 2014 Justin

Day 3 – “Round 2,  Fight!” Highlights of this part include:

  • Use the enumerateChildNodesWithName function to examine every node in the scene
  • How to test if a SKNode is a particular class
  • Use the update method of the GameScene to call an update method in other nodes
Download the Xcode Project (updated for Swift 1.2)

 


Before this video begins I import in another ultra manly “Bro” character from GameArtPartners.com . You can grab any art you want though.

Custom Class vs SKSpriteNode Class

I’ll use the Arnie looking bro as the node we don’t care much about. He’ll be added to the scene to basically skip over when we enumerate through every child node. Previously our SKScene only had one other child, which was an instance of our custom Bro class, so we needed at least one more. Here’s the code from the GameScene class that sets up both…

At #1 we declare a broNode of class Bro to make it accessible in any of our functions.

At #2 we add initialize our broNode and set the imageNamed parameter to a matching name in the Images.xcassets catalog. We then position and add the broNode to the scene using addChild.  The exclamation point means we’re using the unwrapped value of the object, in other words, it’s not an optional value. It better equal something or else…

THIS WILL HAPPEN

…Don’t click that, unless you want to see what will happen. Seriously. I warned you.

At #3 we are adding broNode2 (the Arnie art) which is simply an SKSpriteNode.  I know, I know I’m downplaying the coolness of SKSpriteNodes by saying “simply”.  Even though we don’t really use the .name property in this example project, I included it in the demo video to show how it is possible to identify a node in the scene that hasn’t been declared like the original broNode. For example, this is a perfectly acceptable reference to broNode2 from any function…


The Almighty Update Function

Moving on, lets talk about the update statement, which for game developers is like the knife in a Swiss Army Knife. Or maybe, its the bottle opener. Whichever is the most used.

The GameScene class includes this out of the box…

…which does exactly what it says. Executes code in those brackets before the frame is rendered. Great for doing things like…uhhh, moving $h*t around in the game!  Or a plethora of other game related events. And don’t be shy about using this function to do A LOT. Ideally it wants to execute code here at 60 times per second, and believe me, modern iOS devices can handle it. Every good game developer has pushed the limits of how much can be done this fast, but I think most would tell you that these days it gets harder and harder to trip up the iPhone and iPad with what’s possible (e.g. slow the frame rate down).  Point is, don’t get scared working with your update function.

And speaking of, lets enumerate through every node in the scene…

At #1, we are using the “*” to specify that we want to examine EVERY node in the scene.  Alternatively we could isolate this to specific nodes by name, for example…

And in many cases, you’ll want to do exactly that. In this case, we’ve only got 2 nodes anyway and the point of this exercise is to check the class type of one of them.  In the enumeration block,  any node we find will be referred to as node.

At #2, the if statement is checking to see if the node we found belongs to the Bro class. Let me re-phrase that a little closer to what the line of code actually is because this is pretty cool. Here’s the line again…

Read it like this:  if we can successfully create an instance named foundBro that equals the current node of class type Bro.

And another way to explain the magic that’s happening here: in one single line we’re casting foundBro as this particular node that has a class type Bro.

Keep in mind, in this example we know that’s gotta be our broNode that we declared earlier, the one and only node created from the Bro class..  But things aren’t always that simple. We might have hundreds Bros in the scene. The enumerator would look at each one, and temporarily let foundBro equal that same node, then execute whatever needs executing.  So the code in the brackets of the if statement only runs if casting foundBro was a success.

In which case, we call foundBro.update() . We’ll write this in the Bro class in a moment. 

So what happens to foundBro after each iteration of the enumerator? Nothing. Its here one moment, gone the next. Its not saved (retained).


 

The Update Function in the Bro Class

Back in the Bro.swift class, we should now include an update method of it’s own…

For the sake of giving us something to look at when the project runs, the code above tests if the currentState does not equal .Idle then we change self.position (which moves the entire node) by 1 point on the x axis. So whenever we tap the screen and cycle between states, .Idle, .Hurt, or .Fire  our little dude moves whenever he’s not in an idle state.

 

Day 4 – Learn Swift and Sprite Kit – Initialize with a Dictionary, For Loops and More Physics

 

 

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

0 Comments

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