Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Wiring up the sprite onTap events


Your games will often require the ability to run code when the player taps a specific sprite. I like to implement a system that includes all the sprites in your game so you can add tap events to each sprite without building an additional structure. We have already implemented onTap methods in all of our classes that adopt the GameSprite protocol; we still need to wire up the scene to call these methods when the player taps the sprites.

Note

Before we move on, we need to remove the Core Motion code since we will not be using it in the finished game. Once you finish exploring the Core Motion example, please remove it from the game by following the previous section's bullet points in reverse.

Implementing touchesBegan in the GameScene

SpriteKit calls our scene's touchesBegan function every time the screen is touched. We will read the location of the touch and determine the sprite node in that position. We can check if the touched node adopts our GameSprite protocol...