Exploring SpriteKit

As mentioned before, SpriteKit's main usage is to build two-dimensional games. The SpriteKit framework has been around for quite some time already and it has enabled developers to build many successful games over the years. SpriteKit contains a full-blown physics simulation engine and it is able to render many sprites at a time. A sprite represents a graphic in a game. It could be an image for the player, but also a coin, an enemy, or even the floor that a player walks on. When we mention sprites in the context of SpriteKit, it means that we refer to a node that is visible on the screen.

Because SpriteKit has a physics engine, it can also detect collisions between objects, apply forces to them, and more. This is pretty similar to what UIKit Dynamics is capable of. If you're a bit rusty on what UIKit Dynamics are and how they work, truncated

To render content, SpriteKit uses scenes. These scenes can be considered levels or major building parts of a game. In the context of AR, you will find that you typically only need a single scene. A SpriteKit scene is responsible for updating the position and state of the scene. As a developer, you can hook into this rendering through theĀ SKScene's update(_:) method. This method is called every time SpriteKit is about to render a new frame for your game or ARKit scene. It is important that this method's execution time is as short as possible because a slow implementation of update(_:) method will cause frames to drop, which is considered bad. You should always aim for maintaining a steady 60 frames per second. This means that the update(_:) method should always return in 1/60th of a second.

To begin our exploration of SpriteKit, create a new project in XCode and choose the Game template. Pick SpriteKit as the underlying game technology and give your project a name. For instance, HelloSpriteKit.

When Xcode generates this project for you, you are given some files that you haven't seen before. These are the following:

These two files are pretty much what storyboards are to regular apps. You can use these to set up all nodes for your game scene or to set up reusable actions that you can attach to your nodes. We will not go into these files for now as they are pretty specific to game development. If you are interested in learning all the ins and outs of SpriteKit, I recommend that you pick up Swift 3 Game Development - Second Edition, by Stephen Haney. This book goes in-depth about game development on iOS with SpriteKit.

If you build and run the sample project that Xcode provides, you can tap the screen to make new sprite nodes appear on the screen. Each node performs a little animation before it disappears. This isn't very special in itself, but it does contain a lot of valuable information. For instance, it shows you how to add something to a scene and how to animate it. Let's see exactly how this project is set up so you can apply this knowledge when you build the AR gallery later.