Exploring SpriteKit

SpriteKit is mostly used by developers to build two-dimensional games. SpriteKit has been around for quite some time already, and it has helped developers to create many successful games over the years. SpriteKit contains a full-blown physics simulation engine, and it can render many sprites at a time. A sprite represents a graphic in a game. A sprite could be an image for the player, but also a coin, an enemy, or even the floor that a player walks on. When sprites are mentioned in the context of SpriteKit, it is meant to refer to one of the nodes that are visible on the screen.

Because SpriteKit has a built-in physics engine, it can detect collisions between objects, apply forces to them, and more. This is pretty similar to what UIKit Dynamics is capable of. 

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 the rendering of frames through the update(_:) method of SKScene. This method is called every time SpriteKit is about to render a new frame for your game or ARKit scene. It is essential that this method's execution time is as short as possible, as a slow implementation of the update(_:) method will cause frames to drop, which is considered bad. You should always aim to maintain a steady 60 frames per second. This means that the update(_:) method should always perform its work in less than 1/60th of a second.

To begin exploring SpriteKit, create a new project in XCode and choose the Game template. Pick SpriteKit as the underlying game technology and give the project a name. For instance, SpriteKitDefault, as shown in the following screenshot:

When Xcode generates this project for you, you should notice some new files that you haven't seen before:

  • GameScene.sks
  • Actions.sks

These two files are to SpriteKit games 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 get 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, go ahead 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 in case you wish to build an AR experience with SpriteKit at some point.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.119.251