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
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating labels


In almost every game, there are different elements (scores or text labels) to provide visual information to the player and give them an incentive to keep playing in order to beat its score record. In this section, we are going to learn how to add these informative elements to a scene.

In SpriteKit, we have a class named SKLabelNode that inherits from SKNode and provides all the methods and attributes to load fonts and manage every label that we want to show on the screen. We are going to use this class to add a score label at the top-right side of the screen and update it, as the rabbit avoids wrong doors.

Let's start by creating our first label. Then, we will learn how to update it programmatically. For this purpose, we are going to need a new variable. So, add the following line to GameScene just after the declaration of isCollisionDetected:

private var labelScore: SKLabelNode!

Next, we need to call a method to initialize it. So, add the following line at the end of didMoveToView...