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.
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 or
operation. Second, we've added a gap identifier, a component we haven't defined yet.
A gap is the hole between two pipes, and we need to detect the moment when the bird passes through this hole in order to increase the score.
Let...