17

I'm trying to create a subclass of SKShapeNode in swift as SKShapeNode(circleOfRadius: radius) but there is no designated init for it.

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

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")
        }
    }

In Main Code

let playerOne = Player(rectOfSize: CGSize(width: 100, height: 100))
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Chris
  • 173
  • 1
  • 6
  • Does this help: [Subclassing SKShapeNode with Swift](http://stackoverflow.com/questions/24235185/subclassing-skshapenode-with-swift) ? – Martin R Jan 29 '15 at 20:03
  • 1
    If the compiler croaks about a missing init it usually suggests to insert one (red dot with square). Just let it do that. – qwerty_so Jan 29 '15 at 20:11
  • Martin. I worked with that example earlier and got it working now but how would i make that a circle instead of a rectangle? – Chris Jan 29 '15 at 21:07
  • if you show your code, it will be clearer... – sergio Jan 29 '15 at 21:33

2 Answers2

20

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")
    }

}
hamobi
  • 7,940
  • 4
  • 35
  • 64
  • 4
    Is this how it must be done to subclass SKShapeNode(circleOfRadius: ....)? sprite kit really doesn't have a built in initialiser for this? – Confused Oct 28 '16 at 14:55
4

This worked for us.

It allows you to use other convenience initializers from SKShapeNode, but it has weird syntax explained here: https://stackoverflow.com/a/24536826/144088

class CircleNode : SKShapeNode {

    override init() {
        super.init()
    }

    convenience init(width: CGFloat, point: CGPoint) {
        self.init()
        self.init(circleOfRadius: width/2)
        // Do stuff
     }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439