Making the components interact

Although the app is colorful and seeing the bird fly is fun, we need to create a real-world scene, where collision with an obstacle typically brings you to a halt.

Setting up the collision-detection engine

The SpriteKit physics engine provides us with a really simple mechanism to detect collisions between objects. Basically, we need to set a bitmask for each component and then a collision-detection delegate. Let start defining the bitmask; for it, we define an enumeration in GameScene:

enum BodyType : UInt32 {
    case bird = 0b0001
    case ground = 0b0010
    case pipe = 0b0100
    case gap = 0b1000
}

Pay attention to two things. First, we must define the bitmask as a power of 2 so that we can detect what touches what using a bitwise ...

Get Swift: Developing iOS Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.