Drawing dashed line in Sprite Kit using SKShapeNode

萝らか妹 提交于 2020-05-27 05:02:31

问题


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;

回答1:


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)



回答2:


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)



回答3:


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()




回答4:


Swift 5

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




回答5:


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)
        }
    }
}


来源:https://stackoverflow.com/questions/28072879/drawing-dashed-line-in-sprite-kit-using-skshapenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!