no designated init for SKShapeNode(circleOfRadius: radius)
how's this?
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")
}
}
Subclassing SKShapeNode with Swift
You have two problems with the code you tried:
rectOfSize
inSKShapeNode
takes aCGSize
not aCGPoint
rectOfSize
inSKShapeNode
is a convenience initializer so you won't be able to call it from a subclass. You will have to callsuper.init()
and implement the rect functionality yourself
You can do something like this:
init(rectOfSize: CGSize) {
super.init()
var rect = CGRect(origin: CGPointZero, size: rectOfSize)
self.path = CGPathCreateWithRect(rect, nil)
}
Inherit SKShapeNode
SKShapeNode.init(circleOfRadius:)
is a convenience initializer on SKShapeNode
so you can't call it from a Swift initializer. Swift enforces the designated initializer pattern more strictly than Objective C does.
Unfortunately, it appears the designated initializer for SKShapeNode
is just init
, so you'll need to do something like this:
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")
}
}
The code above works for subclassing SKShapeNode
, but given the API that Apple provides and considering how your code might need to change in the future, it might make more sense to create an SKNode
subclass that contains one or more SKShapeNode
s. In this setup, if you wanted to represent the player as more than just a simple circle, you could simply add additional nodes to the player node.
Setting non-optional properties in an SKShapeNode subclass
You can not call the method addChild
before calling the super.init
so here is the fix,
class MyNode: SKShapeNode {
private var label: SKLabelNode
init(_ frame: CGRect) {
self.label = SKLabelNode(text: "Some text")
super.init()
self.path = CGPath.init(rect: frame, transform: nil)
self.addChild(self.label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
SKShapeNode.Get Radius?
You can use the shape node's path
property to determine the bounding box of the shape with CGPathGetBoundingBox(path)
. From the bounds, you can compute the radius of the circle by dividing the width (or height) by 2. For example,
CGRect boundingBox = CGPathGetBoundingBox(circle.path);
CGFloat radius = boundingBox.size.width / 2.0;
Related Topics
How to Handle Menu Button Action in Tvos Remote
Nscollectionviewitem Never Instantiate
Why Mark Something Final in Swift Except for Architectural Considerations
Hovering a Modelentity in Front of Arcamera
Response Struct Does Not Like Codingkeys
How to Convert Unmanaged<Cfdata> to Nsdata
Unsafemutablepointer<Cftyperef> in Swift 3
Need Explanation About Random Function Swift
How to Get Core Data Entity by It's Objectid
Changing the Color of the Icons in a Uitextfield Inside a Uisearchbar
Is Is Possible to Use Vapor 3 Postgres Fluent in a Standalone Script
Saving Dictionary into Nsuserdefaults
Concatenate Literal with Optional String