Swift - должен вызвать назначенный инициализатор ошибки суперкласса SKSpriteNode
Этот код работал на первой бета-версии XCode 6, но на последней бета-версии он не работает и дает такие ошибки 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)
Я застрял с этим.. что будет самым простым исправлением?
Ответы
Ответ 1
init(texture: SKTexture!, color: UIColor!, size: CGSize)
- единственный назначенный инициализатор класса SKSpriteNode, остальные - все инициализаторы удобства, поэтому вы не можете называть их супер. Измените свой код на это:
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)
}
}
Кроме того, я объединил бы все это в один инициализатор.
Ответ 2
Сумасшедший материал.. Я не совсем понимаю, как мне удалось это исправить.. но это работает:
convenience init() {
self.init(imageNamed:"bubble")
self.hidden = true
}
init(texture: SKTexture!, color: UIColor!, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
добавьте convenience
в init
и удалите init(texture: SKTexture!)