Learn iOS with Examples, Code Snippets and Video Tutorials

Casting in Sprite Kit

Casting in Sprite Kit

This is one of the most important concepts to understand when working with Sprite Kit and in particular with Xcode’s Scene Editor to layout your game. Below is one of the most common things you’ll do in any game: add a Color Sprite (SKSpriteNode) to the scene, give a texture and name it something like Player. 

Casting with Swift and Sprite Kit

What we need to do now is associate that Color Sprite named Player with an SKSpriteNode variable in our code. In your GameScene.swift file, declare a variable named thePlayer like so…

 

Next in the GameScene’s didMoveTo statement (which is already written in the GameScene.swift if you’ve started your project with the Sprite Kit template), we want to tie together thePlayer with the “Player” named sprite. So locate this function…

And add…

So you’ve just done a little casting here. There’s a few ways we could have gone about this, but I like this as its nice and safe. What we’re doing in lay-man’s terms is saying, “hey IF you can find somePlayer named Player that is an SKSpriteNode, then make thePlayer be somePlayer

What happens to somePlayer after we say thePlayer = somePlayer. For a short amount of time, they are the same thing.  So if we set somePlayer.alpha to 0, it would be the same as setting thePlayer.alpha to 0. And now thePlayer is also the same instance as the Color Sprite in our SKS file named “Player”. After this if statement in the didMove function has finished executing somePlayer can no longer be referenced.  That doesn’t mean somePlayer is destroyed, it just means there’s no longer any record of it essentially.

What’s important here is…

  • (1) If by some accident a child in the scene was named “Player” but was NOT an SKSpriteNode, we’ve prevented a crash by doing our casting in an if statement.
  • (2) We now have a declared variable named thePlayer which we can use in ANY function in our GameScene class which will always refer back to the Color Sprite named “Player”. So as long as this scene is active, we don’t ever have to do this casting again.

As you work more with Sprite Kit you’ll learn that crashes can come at you pretty quick if you make assumptions about one thing being something it’s not. For example, when physics bodies are bouncing around and you test collisions between them, all Sprite Kit tells you by defaults is one body hit another. We have to check to see if the body was a SKSpriteNode (or some other custom class we were looking for).


Danger Zone

If you want to play with fire  you could also have written this…

The ! mark means you are going to force the child in the scene named “Player” to be an SKSpriteNode. Notice in our previous example, we had a similar as but it had a question mark afterwards. This is the optional version, and it’s kind of like saying, “well if you can’t do it, don’t worry about it”. 

Forcing it will work, but it could lead to a crash (again, if “Player” wasn’t really an SKSpriteNode).


How to test if something is a particular class in Sprite Kit

The previous line would actually be just fine if it was inside of an if statement like so…

So the if statement is simply say “Hey if Player is an SKSpriteNode, lets do this thing”


So wait, why do I need to do casting?

If you’ve been typing out this in Xcode, you might have noticed that writing…

…actually allows you to add some properties after it.

For example, you could set the alpha like so…

Swift will add that ? even if you leave it off. It has the good sense to think “well, if that child can’t be found, lets make this optional”.  But it has no problem with you setting the alpha property. So why’s that?

Every child node that could possibly be in the scene shares that property. All children are derived from SKNode, and alpha is a property of SKNode. Other common properties are position, zPosition, isHidden, and so on.

It’s when you want to set a property that only an SKSpriteNode has, that you need to do some casting. And as most nodes in a scene are Color Sprites (SKSpriteNodes), you’ll find you do a lot of casting. Even more so once you write your own classes in Sprite Kit.

 

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com