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

A flying bird

Now, it's time to implement our hero.

Adding the Bird node

First of all, we must add a new character to the GameScene class:

class GameScene: SKScene {
    private var bird: Bird!
    //...
    override func didMoveToView(view: SKView) {
        //...
        bird = Bird(textureNames: ["bird1.png", "bird2.png"]).addTo(screenNode)
        bird.position = CGPointMake(30.0, 400.0)

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

We can see that this new class behaves like the other, which we have already implemented:

import SpriteKit class Bird : Startable { private var node: SKSpriteNode! private let textureNames: [String] var position : CGPoint { set { node.position = newValue } get { return node.position } } init(textureNames: [String]) { self.textureNames = textureNames node ...

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