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

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 node tree:

import SpriteKit class Pipes { private class var createActionKey : String { get {return "createActionKey"} } private var parentNode: SKSpriteNode! ...

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