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

2-star challenge: colliding puppets

Before going further, you need to add the code that detect collisions between the rabbit and the puppet. When this happens, the blinking action should be run as well as the sound that is played when the rabbit collides with the wrong door. Also, you should take into account that colliding with the enemies will not affect the score.


I've decided to use a different flag to detect whether a collision with an enemy happened, as we will need it later in this chapter. So, let's add a new variable declaration at the top of GameScene:

private var isEnemyCollisionDetected: Bool = false

Then, we need to write the code that detects these collisions. So, add the following block of code at the end of detectCollisions:

// Check puppet collision
if enemy.puppet.frame.intersects(rabbit.frame) && (enemy.puppet.position.y - enemy.puppet.frame.height/2) <= rabbit.position.y {

    // Collision detected
    isEnemyCollisionDetected = true

    // Reproduce...