Book Image

Swift by Example

By : Giordano Scalzo
Book Image

Swift by Example

By: Giordano Scalzo

Overview of this book

Table of Contents (15 chapters)

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 the 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—apply 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 flapping after touching a pipe, and sometimes the bird starts flying again.

To solve this issue, we add a status variable to the bird. This variable indicates whether the bird...