为什么可以像这样实例化一个SKShapeNode
let circle = SKShapeNode(circleOfRadius: 10)但是,当我想要创建继承表单SKShapeNode的类时,我不能这样做:
public class Player:SKShapeNode{
public var playerName : String
private var inventory: [enumObject]
init(nameOfPlayer:String, position:CGPoint, radious: CGFloat) {
super.init(circleOfRadius: radious)
self.position = position
self.fillColor = SKColor.white
playerName = nameOfPlayer
inventory = [enumObject]()
}
}它说这个init不是为SKShapeNode设计的init,我搜索了它,但是找不到创建这个该死的循环的正确方法。
发布于 2017-03-20 18:42:30
SKShapeNode.init(circleOfRadius:)是SKShapeNode上的一个方便的初始化程序,所以您不能从Swift初始化器调用它。Swift比目标C更严格地执行指定的初始化模式。
不幸的是,SKShapeNode的指定初始化程序似乎只是init,所以您需要这样做:
public class Player: SKShapeNode {
public var playerName : String
private var inventory: [enumObject]
init(nameOfPlayer:String, position:CGPoint, radius: CGFloat) {
playerName = nameOfPlayer
inventory = [enumObject]()
super.init()
self.path = CGPath(ellipseIn: CGRect(origin: .zero, size: CGSize(width: radius, height: radius)), transform: nil)
self.position = position
self.fillColor = SKColor.white
}
// init?(coder:) is the other designated initializer that we have to support
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}上面的代码适用于SKShapeNode子类,但考虑到苹果提供的API,并考虑到您的代码将来可能需要更改,创建一个包含一个或多个SKShapeNode的SKNode子类可能更有意义。在这种设置中,如果您想将播放机表示为不仅仅是一个简单的圆圈,只需向播放机节点添加额外的节点即可。
https://stackoverflow.com/questions/42910524
复制相似问题