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

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

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