gpt4 book ai didi

ios - Swift - 必须调用父类(super class) SKSpriteNode 错误的指定初始值设定项

转载 作者:IT王子 更新时间:2023-10-29 04:59:15 27 4
gpt4 key购买 nike

此代码在第一个 XCode 6 Beta 上运行,但在最新的 Beta 上无法运行并出现此类错误必须调用父类(super class) 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在他们。将您的代码更改为:

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

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

关于ios - Swift - 必须调用父类(super class) SKSpriteNode 错误的指定初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com