Tutorial: Introduction to SpriteKit in Swift

(Image courtesy developer.apple.com)
This is an introduction to a series of SpriteKit tutorials written in the Swift programming language. (We will release the entire series over the course of several weeks). At the top level, this tutorial will be broken up into seven sections. Each section will be comprised of several lessons. For our first section we will focus on the fundamental techniques for working with SpriteKit content. Since sprites are the fundamental building blocks used to create the majority of a games scene’s content we will look at that next. We will then learn how to apply actions to sprites to be able to move around in a game scene. In section four we will expand on the basic concepts just learned by delving deeper into the concept of a node tree and ways to build our game scenes. We will then look at advanced scene processing techniques. In the sixth section we will learn how to simulate physics on the bodies within our scenes. And lastly we will discuss SpriteKit best practices.

Target Audience
We assume that you are already familiar with the fundamentals of app development. You should know how to develop apps using Xcode with Objective-C or Swift programming language, be familiar with blocks or closures and possess an understanding of views and windows as they relate to app development. If you are not familiar with these concepts then we suggest you visit our online learning page and go through some of our Swift programming tutorial before continuing.

SpriteKit Overview
SpriteKit is a graphics rendering and animation framework that comes with a set of tools for animating textured images, or sprites and special effects. SpriteKit conforms to the traditional rendering loop of first processing the content before the frame is rendered. This process of rendering frames is efficient because the work is executed using the graphics hardware (GPU) as opposed to using the CPU. This means that when we position sprites we can arbitrarily do so in each frame of animation without generally needing to worry about time or space complexity. In a nutshell, SpriteKit is a cross-platform (OSX and iOS), efficient and fully optimized game engine.

For the remainder of this introduction we will briefly describe some of the components used to make games. In future tutorial we will explore these and other components in greater detail.
  
Drawing Canvas
An SKView is an object that is responsible for animating and rendering your content once it is placed inside a window object. You may combine the content of this view object with other views that are in the same view hierarchy.

Canvas Content
SpriteKit supports different types of content that include the following:
  1.  Textured or untextured sprites. Sprites are bitmap objects that are drawn on a screen. Texturing is the processes of adding bitmap image(s) to a sprite so that our games "feel" and look lifelike.
  2. Text
  3. CGPath-based shapes. Similar to sprites, CGPath-based shapes are drawn to a screen using points, lines or arcs and may be filled with solid or gradient colors. 
  4. You may also embed video content in a game scene.
Game Scene
An SKScene is an object designed to organize the various sprites and other content within a game. Your game will present one scene at any given time. The processing logic that updates a scene on a per-frame basis will live within this object.

Game Scene Transitions
An SKTransition is an object used for transitioning between multiple SKScene objects when using a single SKView. This object provides default scene transition behavior.

Building Block
The fundamental building block of SpriteKit content are comprised of SKNode objects. These objects come with default behavior that is shared among other node objects. The SKNode class does not draw anything, but instead holds an objects position in a given coordinate system and applies any properties, like rotation, to its content and to the content of its descendants. These descendant relations of nodes are defined by their position in a tree like structure with the scene object acting as the root node. Another benefit to using the SKNode class is that it handles application events like touches. Default event handling by SKNode objects is possible because it is a subclass of the UIResponder class.

Game Action
Animation of content is achieved with the use of SKAction objects. You would generally use actions to move, scale, rotate or animate the transparency of a node. Scenes can also take advantage of these action objects.

Game Physics
SpriteKit comes with a set of objects that define how node objects interact during a collision, under the influence of gravity or in a river stream. These physics objects will speed up your game development by shifting your focus on game dynamics and user interactions instead of getting bogged down with formulating and managing physics equations. To take advantage of these physics objects, you create SKPhysicsBody object and attach them to the nodes in your scene. You can also apply gravity to all, (or some with the use of SKFieldNode), object nodes in your scene by creating a SKPhysicsWorld object and attach it to your scene. If your game includes an animating train, then you can connect the train cars with an SKPhysicsJoint object and simply move the lead car to give it a super real effect.

Conclusion
This concludes our introduction to SpriteKit. After reading this I hope that you are convinced that SpriteKit offers a set of tools that will help you build games comparable to that of a midsize game shop but with the use of third party tools. If you are not convinced, then stay tuned because SpriteKit offers so much more.

In our next blog post we will write code with a focus on the fundamental techniques for working with SpriteKit content.

This tutorial is based on Apple's SpriteKit Programming Guide.



1 comment: