gpt4 book ai didi

Swift SKShapeNode 不接受初始化程序

转载 作者:行者123 更新时间:2023-11-30 11:54:05 26 4
gpt4 key购买 nike

我无法让 SKShapeNode 的子类接受初始化程序。为了解决这个问题,我尝试按照此处发布的示例 Adding Convenience Initializers in Swift Subclass 。我正在使用的代码如下。

class Ground1 : SKShapeNode {

override init() {
super.init()
print("Hello1")
}

convenience init(width: CGFloat, point: CGPoint) {
var points = [CGPoint(x: -900, y: -300),
CGPoint(x: -600, y: -100),
CGPoint(x: -100, y: -300),
CGPoint(x: 2, y: 150),
CGPoint(x: 100, y: -300),
CGPoint(x: 600, y: -100),
CGPoint(x: 900, y: -300)]
self.init(splinePoints: &points, count: points.count)
lineWidth = 5
strokeColor = UIColor.orange
physicsBody = SKPhysicsBody(edgeChainFrom: path!)
physicsBody?.restitution = 0.75
physicsBody?.isDynamic = false
print("Hello2")
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

}

初始化时,便利的 init() 被忽略并且不被使用。导致没有任何形状被添加到场景中。我做错了什么?

最佳答案

感谢0x141E,他给我指明了正确的方向。我发现这可行,但代码看起来很困惑。这里详细解释了以这种方式进行初始化的原因 Adding Convenience Initializers in Swift Subclass 。尽管由于使用 splinePoints 并分配给路径,我无法找到解决方案。

class Ground1 : SKShapeNode {

override init() {
super.init()
}

convenience init(name: String) {
var points = [CGPoint(x: -900, y: -300),
CGPoint(x: -600, y: -100),
CGPoint(x: -100, y: -300),
CGPoint(x: 2, y: 150),
CGPoint(x: 100, y: -300),
CGPoint(x: 600, y: -100),
CGPoint(x: 900, y: -300)]
self.init(splinePoints: &points, count: points.count)
lineWidth = 5
strokeColor = UIColor.orange
physicsBody = SKPhysicsBody(edgeChainFrom: self.path!)
physicsBody?.restitution = 0.75
physicsBody?.isDynamic = false
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

关于Swift SKShapeNode 不接受初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029061/

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