Search code examples
iosswiftcocoa-touchsprite-kitskshapenode

SKShapeNode not drawing UIBezierPath


Calling this code inside the didMoveToView() of my SKScene:

let frame = CGRect(x: 30, y: 200, width: 100, height: 100)
let path = UIBezierPath(rect: frame)
UIColor.blackColor().setStroke()
path.stroke()
self.addChild(SKShapeNode(path: path.CGPath))

does absolutely nothing. The scene is empty (it has the default SKScene grey background). Here I'm just trying to draw a black unfilled rectangle (this is a simplified version of using SKShapeNode to draw a path, the original has more complicated drawing code).


Solution

  • Your shape isn't showing because you haven't given the SKShapeNode a fill or stroke colour. Your code should instead be:

    let path = UIBezierPath(rect: CGRect(x: 30, y: 200, width: 100, height: 100))
    
    let shapeNode = SKShapeNode(path: path.CGPath)
    shapeNode.strokeColor = UIColor.blackColor()
    addChild(shapeNode)