Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Day 2 – Learn Swift and Sprite Kit – Some Physics Properties

Day 2 – Learn Swift and Sprite Kit – Some Physics Properties
October 7, 2014 Justin

Day 2 Highlights

  • Added some physics properties using our old friend SKPhysicsBody
  • Wrote a couple functions, one in our class and one triggered at the end of an SKAction
Download the Xcode Project (update for Swift 1.2)


So today got off to an easier start then yesterday with Swift and Sprite Kit. Still though Swift is doing an excellent job making use of the “Fix It” prompts as I tend to make slight mistakes that are easily fixed. Mostly adding either ? or ! to my variables.

Since our Destroyer class from yesterday is still pretty light on code, I’ll paste in all of it for you to take a look at it as it was at the end of the video above.

At #1 I’ve added a few physics properties. The first being the most important where we initialize a physics body and set it’s shape to a rectangle of the same size as the image we’re using.  Initially the physicsBody?.dynamic property is false (so it is ignored in the physics simulation), but we’ll switch it to true via a function call to the class later. The mass property is pretty irrelevant, I was just demonstrating you can find lots of various physics properties to set at this point.

At #2 I’m creating a function that the GameScene class will be able to call to set the physicsBody’s dynamic property to true. I could set this property directly from the GameScene class, but just to demo that this is also possible via a function, I went that route. Note that this function is public, and by simply writing it, the GameScene class can access it (no need to declare it in a header file…niiiice).

In the GameScene.swift file, here’s what things are looking like by the end of the video…

At #1 we test to see if the destroyerNode is nil or not, so just in case the code in the didMoveToView statement had not actually run and added the destroyerNode. Doubtful that would happen, but if so, then the else statement would just print “that destroyer was not really there” .

Since the destroyerNode is not nil, (it does have a value) the first part of our if statement runs and we go about moving the position to match that of the touch location, set the .velocity to 0,0 and set the .dynamic value to false. This way the node moves to where we touched but won’t take part in the physics simulation until after the upcoming SKAction runs.

At #2 an SKAction is created named waitAction. It has a waitForDuration value of 2 (so 2 seconds).  Then once the destroyerNode runs the action, we specify a completion block (to do something once the wait is over), which simply calls a function named someEvent(). Xcode decided self.someEvent() was more appropriate.

So finally at #3 we write the someEvent function. Nothing is enclosed in our parenthesis after the function name because we are not passing anything into the function (or returning anything from it either). We are simply executing the code inside, which is a call to the function named makeBodyDynamic which we wrote in our Destroyer class.

When everything is said and done, the user can touch the screen, the destroyer art moves to that spot, waits two seconds, then falls down since it becomes dynamic and part of the physics simulation (I assume gravity has a default value pulling downward, as I forget to explicitly set it myself). The code to do all this could obviously be more concise as we didn’t need all those functions, but this was a successful test of simply calling functions.

Baby steps, folks! Got it? Move onto Day 3 – Part 1



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


  1. Grant Christie 6 years ago

    Another great tutorial. I got to wondering about the anchor point. Setting it at it’s feet. How would would we set the anchor point of destroyerNode. Tried destroyerNode.anchorPoint = CGPoint(x:CGFloat(-0.5),y:CGFloat(-0.2)) and destroyerNode.anchorPoint = CGPointZero. No luck.

    • Author
      Justin 6 years ago

      I’ll have to look into that. I think that code looks right, but probably the x value should be left at the default which is .5 (dead center), and then the y should be 0. By the way, I’ve haven’t found that editing the anchorPoint is that necessary unless I need the object to rotate offset from the center. For a character walking, the anchorPoint might not necessarily need the anchor at the feet. Depends on your needs though.

  2. Grant Christie 6 years ago

    whoops! I forgot the “!” – destroyerNode!.anchorPoint = CGPoint(x:CGFloat(-0.5),y:CGFloat(-0.2))

Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com