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

Adding contact events to our game


Now that you are familiar with SpriteKit's physics concepts, we can head into Xcode to implement physics categories and contact logic for our penguin game. We will start by adding in our physics categories.

Setting up the physics categories

To create our physics categories, open your GameScene.swift file and enter the following code at the bottom, completely outside the GameScene class:

enum PhysicsCategory:UInt32 {
    case penguin = 1
    case damagedPenguin = 2
    case ground = 4
    case enemy = 8
    case coin = 16
    case powerup = 32
}

Notice how we double each succeeding value, as in our previous example. We are also creating an extra category for our penguin to use after he takes damage. We will use the damagedPenguin physics category to allow the penguin to pass through enemies for a few seconds after taking damage.

Assigning categories to game objects

Now that we have the physics categories, we need to go back through our existing game objects and...