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)

Pipes!


Now the bird is flapping, but there are no enemies, so the game is pretty boring. It's time to add some obstacles—pipes!

Implementing the pipes node

To implement the pipes as they were in the original game, we need two classes: PipesNode, which contains the top and bottom pipes; and Pipes, which creates and handles PipesNode.

Let's begin with Pipes and add it as an actor to GameScene:

        //...
        let pipes = Pipes(topPipeTexture: "topPipe.png", bottomPipeTexture: "bottomPipe").addTo(screenNode)

        actors = [sky, city, ground, bird, pipes]
        //...

The Pipes class holds the texture name, and it is added to the nodes tree:

import SpriteKit

class Pipes {
    private class var createActionKey : String { get {return "createActionKey"} }
    private var parentNode: SKSpriteNode!
    private let topPipeTexture: String
    private let bottomPipeTexture: String
    
    init(topPipeTexture: String, bottomPipeTexture: String) {
        self.topPipeTexture = topPipeTexture
 ...