I want to create SKShapeNode at a higher level than touchsBegan from SpriteNode, so when I want to add SKShapeNode to the screen from the touchhesBegun event on this sprite, the form already exists, and I just add it to the screen from the inside of the touchsBegan override.
TL DR, in my SKSpriteNode, I'm trying to pre-build an SKShapeNode that will be used as an animated ring when Sprite touches.
I would like to create an SKShapeNode with variable / constants, so I can easily edit its values ​​...
So, in the root of the subclass, I have variables for color, size and line width, ready to use when creating SKShapeNode ...
class Balls: SKSpriteNode { var ringSize: CGFloat = 64 var ringColor: SKColor = SKColor.white var ringWidth: CGFloat = 16 ....
Further down, but still at the root of the class, I create my own ring:
let ring = SKShapeNode(circleOfRadius: ringSize)
And immediately I greet with a mysterious love:
You cannot use an instance of the "ringSize" member in a property initializer. Property initializers start before "I."
Fine OK. I understood. You want to think that a functional class call to create a property must be executed before the values ​​are assigned to self. Neither here nor there, I think I get the trick and can get around this by wrapping everything in a function:
class Balls: SKSpriteNode { var ringSize: CGFloat = 64 var ringColor: SKColor = SKColor.white var ringWidth: CGFloat = 16 var myRing = SKShapeNode() func createRing() -> SKShapeNode{ let ring = SKShapeNode(circleOfRadius: ringSize) ring.strokeColor = ringColor ring.lineWidth = ringWidth return ring }
It makes no mistakes, and my excitement builds.
So, I am adding another line to create the actual ring: ....
myRing = createRing()
Dead again:
! expected declaration
I absolutely do not understand what this means and began to randomly try to wander.
One of them goes into my already messy convenience initializer and adds myRing = createRing()
there ... and this WORKS!
How and why does this work, and is this the best / right / right way to bypass the initialization stream?
:: EDIT :: UPDATE :: Full code context ::
Here is a complete class with my weird and obscure initializers.
import SpriteKit class Circle: SKSpriteNode { var ringSize: CGFloat = 96 var ringColor: SKColor = SKColor.white var ringWidth: CGFloat = 8 var myRing = SKShapeNode() override init(texture: SKTexture?, color: UIColor, size: CGSize) { super.init(texture: texture, color: color, size: size) } convenience init() { self.init(color: SKColor.clear, size: CGSize(width: 100, height: 100)) myRing = createRing() addChild(myRing) print("I'm on the screen") explodeGroup = create_explosionActionGroup() } convenience init(color: UIColor, size: CGSize, position: CGPoint) { self.init(color: color, size: size) self.position = position myRing = createRing() explodeGroup = create_explosionActionGroup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func createRing() -> SKShapeNode{ let ring = SKShapeNode(circleOfRadius: ringSize) ring.strokeColor = ringColor ring.lineWidth = ringWidth return ring }