Swift SpriteKit初始化程序错误不断出现

时间:2015-04-08 02:17:00

标签: swift sprite-kit

我在我的类中写了一个Init,但错误一直出现在init中,有三个错误: 1.需要一个override关键字; 2.必需的初始化程序必须由子类提供; 3.初始化程序只能在一个类型中声明;

这是我的代码:

import SpriteKit


class GameScene: SKScene {
var man:SKSpriteNode
var runningManTextures = [SKTexture]()

override func didMoveToView(view: SKView) {
}


func loadMan() {
    loadManTextures()
    man.position.y -= man.size.height/2
    addChild(man)
}

func loadManTextures() {
    var runningAtlas = SKTextureAtlas(named: "man")
    for i in 1...3 {
        var textureName = "man_\(i)"
        var temp = runningAtlas.textureNamed(textureName)
        runningManTextures.append(temp)
    }
}

init(size:CGSize){

    super.init(size: size)
}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
    }
}


override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

}

1 个答案:

答案 0 :(得分:1)

此代码在Swift中是非法的:

class GameScene: SKScene {
    init(size:CGSize){
        super.init(size: size)
    }
}

要使其合法,请按以下方式编写:

class GameScene: SKScene {
    override init(size:CGSize){
        super.init(size: size)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

您的实际代码将仍然非法,因为您的init(size:)方法未初始化您的man属性,但修复此问题非常简单且在任何情况下都是一个不同的问题。