没有为SKShapeNode指定指定的init(circleOfRadius:radius) [英] no designated init for SKShapeNode(circleOfRadius: radius)
问题描述
我试图迅速创建一个SKShapeNode的子类,作为SKShapeNode(circleOfRadius:radius),但是没有为其指定初始化.
I'm trying to create a subclass of SKShapeNode in swift as SKShapeNode(circleOfRadius: radius) but there is no designated init for it.
任何人都有任何解决方法或有关为什么的信息?我不确定这是错误还是故意的.我发现此视频演示了SKSpriteNode的解决方法,但不适用于我. https://skillsmatter.com/skillscasts/5695-how-to-subclass -a-skspritenode
Anyone have any workarounds or info about why? I'm not sure if this is a bug or intentional. I found this video demonstrating a workaround for SKSpriteNode but its not working for me. https://skillsmatter.com/skillscasts/5695-how-to-subclass-a-skspritenode
总的来说,我正在尝试为SKShapeNode创建一个子类,然后我可以再次从该子类中继承其子版本,以具有不同的版本,从而更轻松地管理我的代码. TIA
Overall i am trying to make a subclass for an SKShapeNode that i can then subclass from again to have different versions of to easier manage my code. TIA
谢谢马丁,我在前面找到了这个例子.它可以工作,但是我如何将其变成圆形而不是矩形?
Thanks Martin i found that example earlier. It works but how would i make that into a circle instead of a rectangle?
import Foundation
import SpriteKit
class Player : SKShapeNode {
override init() {
super.init()
self.name = "Player"
self.fillColor = UIColor.blackColor()
}
init(rectOfSize: CGSize) {
super.init()
var rect = CGRect(origin: CGPointZero, size: rectOfSize)
self.path = CGPathCreateWithRect(rect, nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在主代码中
let playerOne = Player(rectOfSize: CGSize(width: 100, height: 100))
推荐答案
这是怎么回事?
class Player: SKShapeNode {
init(circleOfRadius: CGFloat){
super.init()
let diameter = circleOfRadius * 2
self.path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: diameter, height: diameter)), nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这篇关于没有为SKShapeNode指定指定的init(circleOfRadius:radius)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!