这段代码适用于第一个XCode 6 Beta,但在最新的Beta中,它不起作用,并显示以下错误 Must call a designated initializer of the superclass SKSpriteNode :
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)
我坚持下去..最简单的解决方法是什么?
init(texture: SKTexture!, color: UIColor!, size: CGSize)是SKSpriteNode类中唯一指定的初始值设定项,其余都是方便的初始值设定项,因此您不能在它们上调用super。将代码更改为此:
init(texture: SKTexture!, color: UIColor!, size: CGSize)
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) } }
此外,我会将所有这些整合到一个初始化器中。