No Designated Init for Skshapenode(Circleofradius: Radius)

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:

  1. rectOfSize in SKShapeNode takes a CGSize not a CGPoint
  2. rectOfSize in SKShapeNode is a convenience initializer so you won't be able to call it from a subclass. You will have to call super.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 SKShapeNodes. 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



Leave a reply



Submit