Drawing dashed line in Sprite Kit using SKShapeNode
Asked Answered
K

5

6

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;
Kassala answered 21/1, 2015 at 17:6 Comment(3)
Where do you fail? Show us your try.Tissue
please add that code to your question.Tissue
question is very badly asked. code is poorly formattedCulminant
O
12

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)
Ottillia answered 21/1, 2015 at 17:24 Comment(1)
In Swift 3 CGPathCreateCopyByDashingPath has been replaced by path.copy(dashingWithPhase:lengths:)Reichenberg
H
7

In swift 4:

    let square = SKShapeNode(rectOf: CGSize(width: 64, height: 64))
    let pattern : [CGFloat] = [4.0, 4.0]

    let dashed = square.path?.copy(dashingWithPhase: 1, lengths: pattern)

    let shapeNode = SKShapeNode(path: dashed!)
    shapeNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    shapeNode.fillColor = SKColor.clear
    shapeNode.strokeColor = SKColor.red
    shapeNode.lineWidth = 2
    self.addChild(shapeNode)
Hodgson answered 20/12, 2018 at 12:15 Comment(0)
K
2

For anyone looking to work out how to apply this same principal to an SKShapeNode like I was, here is an example. A rectangle with a blue dashed line.

import SpriteKit
import GameplayKit

let square = SKShapeNode(rectOfSize: CGSize(width: 64, height: 64))
let circle = SKShapeNode(circleOfRadius: 20.0)

var pattern : [CGFloat] = [2.0, 2.0]
let dashed = CGPathCreateCopyByDashingPath (square.path, nil, 0, pattern, 2)

var shapeNode = SKShapeNode(path: dashed!)
shapeNode.fillColor = UIColor.blueColor()
shapeNode.strokeColor = UIColor.blueColor()

A square with a dashed line

Kornegay answered 17/8, 2016 at 11:33 Comment(0)
E
2

Swift 5

if let path = path?.copy(dashingWithPhase: 1, lengths: [5, 5]) {
    let line = SKShapeNode(path: path)
    line.strokeColor = .white
    self.addChild(line)
}

enter image description here

Exacerbate answered 27/1, 2020 at 12:9 Comment(1)
whilst this looks great, for me it lacks some extra information that actually helps you produce the path in the image. where are the points that define that curve, and where does "path" come from in the first place?Na
N
1

Adding to the solution provided by Mike Glukhov above, this is my solution:

func drawTrace(pointArray: Array<CGPoint>) {
    if pointArray.count > 1 {
        let pattern : [CGFloat] = [10.0, 10.0]

        let path = CGMutablePath.init()

        // start at the first point.
        path.move(to: pointArray[0])

        // now add all of the others.
        for p in 1 ..< pointArray.count {
            path.addLine(to: pointArray[p])
        }

        // create the dashed path.
        let dashedPath = path.copy(dashingWithPhase: 1, lengths: pattern)

        let dashName = "dash"

        // now create the node
        let line = SKShapeNode(path: dashedPath)
        line.strokeColor = .white
        line.name = dashName

        if let parent = self.parent {
            if let oldLine = parent.childNode(withName: dashName) {
                oldLine.removeFromParent()
            }

            parent.addChild(line)
        }
    }
}
Na answered 4/5, 2020 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.