ios Swift - 必须调用超类 SKSpriteNode 错误的指定初始值设定项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25164313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - Must call a designated initializer of the superclass SKSpriteNode error
提问by Kosmetika
This code worked on first XCode 6 Beta, but on latest Beta it's not working and gives such errors Must call a designated initializer of the superclass 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)
}
}
and that's how this class is initialiazed:
这就是这个类的初始化方式:
let creature = Creature()
creature.anchorPoint = CGPoint(x: 0, y: 0)
creature.position = CGPoint(x: Int(posX), y: Int(posY))
self.addChild(creature)
I'm stuck with it.. what will be the easiest fix?
我被它困住了..最简单的解决方法是什么?
回答by Epic Byte
init(texture: SKTexture!, color: UIColor!, size: CGSize)
is the only designated initializer in the SKSpriteNode class, the rest are all convenience initializers, so you can't call super on them. Change your code to this:
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)
}
}
Furthermore I would consolidate all of these into a single initializer.
此外,我会将所有这些合并到一个初始值设定项中。
回答by Kosmetika
Crazy stuff.. I'm not fully understand how I managed to fix it.. but this works:
疯狂的东西..我不完全明白我是如何设法修复它的..但这有效:
convenience init() {
self.init(imageNamed:"bubble")
self.hidden = true
}
init(texture: SKTexture!, color: UIColor!, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
add convenience
to init
and remove init(texture: SKTexture!)
添加convenience
到init
和删除init(texture: SKTexture!)