Swift - 必须调用父类 SKSpriteNode 的指定初始化器错误

72

这段代码在第一个XCode 6测试版上工作正常,但在最新的测试版上不起作用并显示以下错误必须调用超类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)
  }
}

这就是这个类的初始化方法:

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

我被卡住了..最简单的解决方法是什么?

2个回答

99

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)
    }
}

此外,我会将所有这些内容整合到一个初始化器中。

我通过重写方便的“init”方法来解决了这个问题 - https://dev59.com/z18e5IYBdhLWcg3w-OYn#25164687 - Kosmetika
@grfs:非常有用的回答,但我很好奇:在哪里记录了这是SKSpriteNode的唯一指定初始化程序? - drewblaisdell
@drewblaisdell https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKSpriteNode_Ref/index.html#//apple_ref/occ/instm/SKSpriteNode/initWithTexture:color:size:@drewblaisdell https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKSpriteNode_Ref/index.html#//apple_ref/occ/instm/SKSpriteNode/initWithTexture:color:size: - Epic Byte
3
如果你看一下初始化器的声明,你会发现方便的初始化器是用 convenience 关键字标记的,而指定的初始化器则没有。这不是非常清楚,因为你需要点击每个初始化器并查看其声明方式。希望未来能够开发出更好的显示方式。 - Epic Byte
4
我希望我能给这个答案投两次赞,因为它让我完全理解了便利初始化器和超类初始化器之间的联系。感谢 @Epic Byte。 - Shawn J. Molloy
回答不错,但是假设你希望它的工作方式与现有的方便初始化器相同,则颜色参数应默认为SKColor.whiteColor()而不是UIColor.clearColor()。 - SwampThingTom

14

太疯狂了...我不完全明白我是如何设法修复它的...但是这个方法有效:

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并删除init(texture: SKTexture!)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接