Learn iOS with Examples, Code Snippets and Video Tutorials

Day 3 – Learn Swift and Sprite Kit – Part 1

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

Day 3 – The saga continues. Highlights of Part 1

  • We start with a new project using some Bro-tastic game art assets from GameArtPartners.com (there’s a Chuck Norris style character for free if you want to import that)
  • We take a look at using enum types to set a “current state of being” for a potential game character or enemy
  • We add a function to a subclass of SKSpriteNode to cycle through those states every time the screen is tapped (in Part 2, we will use these states to change the SKTexture and also move the sprite

 

So its the third day in this series and I’ve broken today’s Swift training down to two different video lessons. As we go forward in this series, I’ll probably paste less in less here in written format as our classes will end up being lengthy, but I’ll discuss some of the bits and pieces here since a lot of these examples can be taught in bite-sized chunks.

This first chunk o’ fun comes from a new Bro class. Take a look at the enum definition first…

What I’m doing here is creating three states to test: Idle, Hurt or Fire which are our enumeration members   (and by the way, for Fire I meant “firing” a weapon, not the stop-drop-n-roll kind of “fire” ).  Then in the last line I’m setting a variable named currentState to BroStates.Idle

The enumerations members are set to a type of Int, or integer. In Swift,  I could have left that out entirely but conceptually I think its easier to understand that each of these members essentially just equals a simple number value.. So you could test the value of those three states in their raw form which by default is 0, 1, or 2 for Idle, Hurt and Fire respectively. Alternatively you could set those enumeration values to a specific number like so…

Also notice those case statements can each be their own line.  And as another alternative,  this also works…

Obviously our first version of this is the most concise and although I’m not always a fan of brevity in code, I can’t argue against it here. We really don’t need to use or know the int values anyway, so letting them be 0,1,2 is fine.  But if you were curious to spit back out their values you could write….

The official Swift documentation  has plenty more examples on enumerations so I won’t beat it to death here.

The video also looks at testing the currentState in a switch statement, which won’t look much different than its Objective-C counterpart. I put the switch statement inside of a function called stepState which is called by the GameScene class. Here’s that snippet in its entirety (as it looks at the end of Part 2)

Every time stepState is called, we check the value of currentState against all of its possible values, which is really just 3 scenarios, Idle, Hurt or Fire. So here’s the breakdown using # mark annotations in the code above.

#1 We write switch currentState followed by opening and closing brackets for all of our potential cases, or values of currentState.

#2 Write  case then .Idle:  as our first potential value for currentState. If it does equal .Idle we execute ONLY the code that follows after the colon and before the next case.

#3 In each case we print a message to the output window telling us what the state was at the time the function was called, then we step the value to the next possible state. So if the state was .Idle we set it to .Hurt. And if it was .Hurt, we set it to .Fire. Eventually .Fire sets it back to .Idle again. Each time the SKTexture is set to a new image which changes the appearance of the SKSpriteNode.

#4 Because we wrote cases for each of our three potential states, the default: statement will never get run, but if we had left out case .Fire for example, we could safely run this switch statement and if the currentState equaled .Fire the default: statement would come into play.

Back in the GameScene class, we call the stepState function in the touchesBegan function…

Continue onto Day 3 – Part 2

 

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

2 Comments

  1. michael 3 years ago

    This is great justin
    cant wait to see a tiled tutorial with swift

    • Author
      Justin 3 years ago

      Ha, working on that now actually! Just had to jump over hurdles going from Xcode 6.x to 6.1. What a mess that was at first. Fortunately most was just adjusting toRaw() to rawValue .I guess both options can’t co-exist. Little frustrating though, these last minute changes. They don’t help us instructors that want to say, record videos or write books and have some code that isn’t outdated immediately.

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