Swift – 必须调用超类SKSpriteNode错误的指定初始值设定项

此代码在第一个XCode 6 Beta上工作,但是在最新的Beta版本中,它不起作用,并且出现这样的错误Must call a designated initializer of the superclass SKSpriteNode

 import SpriteKit class Creature: SKSpriteNode { var isAlive:Bool = false { didSet { self.hidden = !isAlive } } var livingNeighbours:Int = 0 init() { // throws: must call a designated initializer of the superclass SKSpriteNode super.init(imageNamed:"bubble") self.hidden = true } init(texture: SKTexture!) { // throws: must call a designated initializer of the superclass SKSpriteNode super.init(texture: texture) } init(texture: SKTexture!, color: UIColor!, size: CGSize) { super.init(texture: texture, color: color, size: size) } } 

这就是这个class级的初始状态:

 let creature = Creature() creature.anchorPoint = CGPoint(x: 0, y: 0) creature.position = CGPoint(x: Int(posX), y: Int(posY)) self.addChild(creature) 

我坚持它..什么将是最简单的修复?

init(texture: SKTexture!, color: UIColor!, size: CGSize)是SKSpriteNode类中唯一指定的初始值设定项,其余都是便捷初始值设定项,所以不能对它们调用super。 将您的代码更改为:

 class Creature: SKSpriteNode { var isAlive:Bool = false { didSet { self.hidden = !isAlive } } var livingNeighbours:Int = 0 init() { // super.init(imageNamed:"bubble") You can't do this because you are not calling a designated initializer. let texture = SKTexture(imageNamed: "bubble") super.init(texture: texture, color: UIColor.clearColor(), size: texture.size()) self.hidden = true } init(texture: SKTexture!) { //super.init(texture: texture) You can't do this because you are not calling a designated initializer. super.init(texture: texture, color: UIColor.clearColor(), size: texture.size()) } init(texture: SKTexture!, color: UIColor!, size: CGSize) { super.init(texture: texture, color: color, size: size) } } 

此外,我会将所有这些合并成一个初始化程序。

疯狂的东西..我不完全了解我如何设法解决它..但这个工程:

 convenience init() { self.init(imageNamed:"bubble") self.hidden = true } init(texture: SKTexture!, color: UIColor!, size: CGSize) { super.init(texture: texture, color: color, size: size) } 

init增加convenience并删除init(texture: SKTexture!)