Search code examples
swiftsprite-kitcgpathskshapenode

Drawing dashed line in Sprite Kit using SKShapeNode


I fount this solution but I can't make it into swift code

This what I try

var pattern[2]:CGFloat; this 

var dashed: CGPathRef = CGPathCreateCopyByDashingPath(CGPathCreateCopyByDashingPath(path, transform, phase, lengths, count);

var myShapeNode: SKShapeNode!;

        var CGPathCreateCopyByDashingPath:CGPathRef;

Solution

  • This is how you can draw a dashed line in swift. You can change the parameters as you want.

    let bezierPath = UIBezierPath()
    let startPoint = CGPointMake(0, 250)
    let endPoint = CGPointMake(450, 250)
    bezierPath.moveToPoint(startPoint)
    bezierPath.addLineToPoint(endPoint)
    
    var pattern : [CGFloat] = [10.0, 10.0]
    let dashed = CGPathCreateCopyByDashingPath (bezierPath.CGPath, nil, 0, pattern, 2)
    
    var shapeNode = SKShapeNode(path: dashed)
    shapeNode.position = CGPointMake(100, 100)
    self.addChild(shapeNode)