1

I am creating a Circle SKShapeNode class and I am trying to initialize a circle, but it gives me an error on the super.init line. Is there another way that I can initialize the circle? Thanks!

class Circle : SKShapeNode {

    var radius : CGFloat!

    init (pos:CGPoint,rad:CGFloat,color:UIColor) {

        super.init(circleOfRadius: 30)


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Entitize
  • 4,553
  • 3
  • 20
  • 28

1 Answers1

1

I know this is a little old, but I just ran into the same problem with SKScene and thought I might translate...

You are seeing an error here because you are attempting to call a convenience function from the super class. The rules state that you can only call a super designated initializer from a subclass. For convenience functions, you must use 'self'. As in:

self.init( circleOfRadius: 30 )

However, if you have also partially implemented an override for the init?(coder:) function. Your subclass only inherits the superclass convenience initializers if you either implement all or none of the designated initializers. So, delete the required init?(coder:) function.

That should leave you with the (really strange) error:

'self' used before self.init call.

Maddeningly, that error occurs on the call to the convenience function self.init(circleOfRadius:)

The answer is found in the link from Darvydas, in the second answer: apparently, the construction of the SKxxxxx objects is somewhat broken. You need to directly call the designated initializer init() before calling the convenience one. This should work for you:

    class Circle : SKShapeNode {

    var radius : CGFloat!

    convenience init (pos:CGPoint,rad:CGFloat,color:UIColor) {
        self.init()
        self.init(circleOfRadius: 30)
    }

}

Hope that helps you (or someone...)

PRB
  • 484
  • 2
  • 5