Can't see my SKShapeNode when creating a custom class

860 views Asked by At

I've been working on a project with quite a lot of nodes, and am finding that I'm having to create an SKNode with an SKShapeNode added as a child to it. But I feel like I should be able to just be able to initialise a custom class of SKShapeNode.

When I do (code below), when I add the class to my GameScene as a child it registers its there (the physics body is there) but the I can't see the shape.

import Foundation
import SpriteKit

class WallNeut: SKShapeNode {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(radius: CGFloat) {

        super.init()

        zPosition = 10
        lineWidth = 5
        strokeColor = SKColor.grayColor()

        physicsBody = SKPhysicsBody(circleOfRadius: radius)
        physicsBody?.affectedByGravity = false
        physicsBody?.allowsRotation = false
        physicsBody?.pinned = true

    }

}
1

There are 1 answers

2
The Tom On BEST ANSWER

You are calling super.init() which is the designated initializer but are nowhere defining the path you want your SKSpriteNode to follow.

This can be done in your custom init by adding :

path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: radius * 2, height: radius * 2)), nil)

for a circle you may also want to fill it with :

fillColor = NSColor.grayColor()