子類化 SKSpriteNode

你可以子類 SKSpriteNode 並定義你自己的精靈型別。

class Hero: SKSpriteNode {
    //Use a convenience init when you want to hard code values
    convenience init() {
        let texture = SKTexture(imageNamed: "Hero")
        self.init(texture: texture, color: .clearColor(), size: texture.size())
    }

    //We need to override this to allow for class to work in SpriteKit Scene Builder
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }

    //Override this to allow Hero to have access all convenience init methods
    override init(texture: SKTexture?, color: UIColor, size: CGSize) 
    {
        super.init(texture: texture, color: color, size: size)
    }
}