Book Image

Getting Started with SpriteKit

By : Jorge Jordán
Book Image

Getting Started with SpriteKit

By: Jorge Jordán

Overview of this book

SpriteKit is Apple’s game engine to develop native iOS games. Strongly boosted by the Apple Inc., Cupertino, it has increased in popularity since its first release. This book shows you the solutions provided by SpriteKit to help you create any 2D game you can imagine and apply them to create animations that will highlight your existing apps. This book will give you the knowledge you need to apply SpriteKit to your existing apps or create your own games from scratch. Throughout the book, you will develop a complete game. The beautiful designs implemented in the game in this book will easily lead you to learn the basis of 2D game development, including creating and moving sprites, and adding them to a game scene. You will also discover how to apply advanced techniques such as collision detection, action execution, playing music, or running animations to give a more professional aspect to the game. You will finish your first game by learning how to add a main menu and a tutorial, as well as saving and loading data from and to the player’s device. Finally, you will find out how to apply some mobile games techniques such as accelerometer use or touch detection.
Table of Contents (13 chapters)
Getting Started with SpriteKit
About the Author
About the Reviewer


I think that I'm not wrong if I say that we as developers have the healthy (or unhealthy) habit of trying to decompose mentally (or even physically) everything that comes to our hands into smaller pieces to understand how it works.

In my case, I have to acknowledge that I am a video game lover and a restless developer, and every time I get a game or an app, I can't stop myself from thinking about how an animation or a particular effect is achieved.

This concern helped me learn how to develop apps and games. Thanks to this, I discovered that Apple provides a powerful set of tools that can be combined to create amazing games and stunning apps.

One of these tools is SpriteKit, a 2D game engine that is at the forefront of the frameworks that are available on the market and which can be included in any application to provide its dynamic features.

If you are reading these lines, you probably are either a game lover with a curiosity to learn how video games are developed, or you are an app developer who wants to know how the animations that you have seen in other apps or games have been created.

At this point starts a journey full of new experiences and knowledge that will satisfy all your curiosity. So take a seat, get comfortable, and start reading and enjoying.

What this book covers

This book contains six chapters that will guide you through the process of creating a video game with the tools provided by SpriteKit. At the same time, you will learn how to deal with the common difficulties that you may come across when developing games and how to apply the techniques that you learn here to give a stunning look to an app.

Chapter 1, The First Steps toward SpriteKit, covers the basics of developing a game with SpriteKit. In this chapter, you will learn what a game engine is and what a new SpriteKit project looks like. In addition to this, you will learn the purpose of the SKNode and SKScene classes and how to utilize them to add a background and a sprite into the game.

Chapter 2, What Makes a Game a Game?, shows the main techniques used in game development, such as moving sprites on the scene, detecting touches, and handling collisions. In addition to this, you will learn how to create and update labels and play music and sound effects.

Chapter 3, Taking Games One Step Further, teaches advanced techniques such us how to create complex nodes by extending the SKNode class or implement the parallax effect. You will also learn how to draw geometrical primitives and animate sprites.

Chapter 4, From Basic to Professional Games, helps us provide our game with the needed components to consider it as a finished product. You will learn how to create a Game Over condition for the game and a main menu scene from where you will learn how to transition to a tutorial that you will create for players so that they know the mechanism of the game. You will also learn how to save and load data from internal and external sources.

Chapter 5, Utilizing the Hardware and Graphics Processor, teaches you how to get the most advantage from the hardware of physical devices using the accelerometer or creating stunning visual effects by adding shaders, lights, and shadows into a game.

Chapter 6, Auxiliary Techniques, covers the development of special effects, which are also known as particle systems, and ways to combine SpriteKit with UIKit in order to create robust games and dynamic apps. You will also learn how to use third-party tools to create custom audio and fonts and find resources that can be used in games.

What you need for this book

To follow the implementation of the code provided in this book, you will need the following hardware and software:

  • An Intel-based Mac that runs on Mac OS X 10.10.4 or later

  • The latest version of Xcode (version 7.0 at the time of writing this book)

  • An enrollment in the iOS Developer Program if you want to test the games on a device

  • An iOS device to test games on it

You will not need a lot of experience developing with Swift or SpriteKit as the chapters will help you easily understand how to utilize the tools provided by the game development framework.

Who this book is for

If you are an iOS developer who wants to learn which tools offer the game development framework provided by Apple so that you can add an extra edge to your applications or learn how to bring to life the games that you have imagined in your mind, this book is for you. It has been written to teach the key concepts of 2D game development and ways to create 2D games using SpriteKit. This book will help you deploy framework tools to improve the visual experience of your apps.


There are several text styles in this book that refer to different type of information. For example, you will find words referring code that will look like the following line:

There are a couple of storyboard files, namely Main and LaunchScreen, which are responsible for showing the game screen and launch image respectively.

You will find code blocks that look like this:

import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true

If you are expected to perform some actions on specific places of the screen you will find the instructions highlighted in the following way:

To create a new SpriteKit project, we need to open Xcode and then navigate to File | New | Project….

You will find instructions specifying the exact value you have to enter in a text field that will look like the following line:

Call it Enemy and choose the folder where you want it to be saved.

Hints, tips, advices and notes will look like this:


Note that we just need to create a motion manager instance as thanks to it we can retrieve all the motion data we need.

Sometimes you are going to be challenged by me, requesting you to try to solve a particular situation. These challenges will be labeled as 1-star, 2-star or 3-star Challenge depending on the difficulty of the task, but they can be solved with the knowledge acquired along the book:

2-star challenge: colliding puppets

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.