Completing the game

Almost everything is done now, and in this final section, we are going to add the correct interaction between all the elements of the game.

Colliding with pipes

When the bird touches a pipe, we need to push it down so that it touches the ground and dies:

extension GameScene: SKPhysicsContactDelegate {
    func didBeginContact(contact: SKPhysicsContact!) {
    //...
    case BodyType.pipe.rawValue | BodyType.bird.rawValue:
    println("Contact with a pipe")
    bird.pushDown()
    //...
}

To push it, we can use the same technique that we used for the flapping—applying an impulse:

    func pushDown() {
        dying = true
        node.physicsBody!.applyImpulse(CGVector(dx: 0, dy: -10))
    }

Although the impulse has been applied correctly, you might notice that you can continue ...

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.