O'Reilly logo

Swift 2 By Example by Giordano Scalzo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required