首页 > 解决方案 > SKShapeNode Strokes show gaps when drawn with CGPath

问题描述

I am trying to draw a shape using CGPath and SKShapeNode. However, whatever the lineJoin or lineCap combinations I try to use, the lines have gaps.

enter image description here

import PlaygroundSupport
import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        super.didMove(to: view)
        let path = CGMutablePath()
        path.move(to: NSPoint(x: -66.585, y: 1.125))
        path.addCurve(to:  NSPoint(x: -60.585, y: -41.765), control1: NSPoint(x: -66.585, y: 1.125), control2: NSPoint(x: -65.835, y: -32.735))
        path.addCurve(to:  NSPoint(x: 66.585, y: -20.3150000000001), control1: NSPoint(x: -60.585, y: -41.765), control2: NSPoint(x: -3.39500000000001, y: -1.88499999999999))
        path.addCurve(to:  NSPoint(x: 56.545, y: 18.235), control1: NSPoint(x: 66.125, y: -7.09500000000003), control2: NSPoint(x: 62.695, y: 6.16499999999996))
        path.addCurve(to:  NSPoint(x: -66.585, y: 1.125), control1: NSPoint(x: 56.055, y: 19.1849999999999), control2: NSPoint(x: -15.415, y: 41.765))
        path.closeSubpath()
        let shapeNode = SKShapeNode(path: path)
        shapeNode.fillColor = .red
        shapeNode.lineWidth = 10
        shapeNode.lineJoin = .round
        shapeNode.strokeColor = .white
        addChild(shapeNode)
    }
}

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
    scene.scaleMode = .aspectFill
    sceneView.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

What am I doing wrong here?

标签: swiftmacossprite-kitskshapenode

解决方案


这对我来说看起来很奇怪:

    path.move(to: NSPoint(x: -66.585, y: 1.125))
    path.addCurve(to:  NSPoint(x: -60.585, y: -41.765), control1: NSPoint(x: -66.585, y: 1.125), control2: NSPoint(x: -65.835, y: -32.735))

你移动到 (-66.58., 1.125)。然后添加一条曲线,其中第一个点为 (-60.585, -41.765),第一个控制点与您移动到的原始点相同。似乎这会导致一些奇怪的问题。我认为你想让你移动的点成为曲线的第一个点,而不是它的控制点。


推荐阅读