Swift - 来自用户绘制图像的SKShapeNode(使用CGMutablePath())

时间:2017-06-29 02:12:36

标签: swift swift3 drawing cgpath skshapenode

大家好!

我敢打赌,这是一个简单的解决办法,但我无法理解它。在我正在制作的游戏中,我希望用户能够绘制某些东西(波浪形,圆形等)并从该绘图中制作SKNode(SpriteNode,ShapeNode等)。我几乎让它工作了,但是当我在屏幕上画画时,它会在屏幕右上角的一个随机点开始绘制并垂直翻转我的手指动作(但不是水平)。要真正了解我的问题,您可能需要查看它,所以这是我的GameScene.swift代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var lastPoint = CGPoint.zero
var swiped = false

var ref = CGMutablePath()
var lineDrawing = SKShapeNode()

override func didMove(to view: SKView) {

    lineDrawing = SKShapeNode(rectOf: self.frame.size)
    lineDrawing.lineWidth = 10
    lineDrawing.position = CGPoint.zero
    self.addChild(lineDrawing)

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false

    if let touch = touches.first {

        lastPoint = touch.location(in: self.view)

    }
}



func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

    ref.move(to: fromPoint)
    ref.addLine(to: toPoint)

    lineDrawing.path = ref
    lineDrawing.lineCap = CGLineCap.round
    lineDrawing.lineWidth = 10
    lineDrawing.strokeColor = SKColor.black//maybe .strokeTexture for chalk

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    if let touch = touches.first {
        let currentPoint = touch.location(in: view)
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)

        // 7
        lastPoint = currentPoint
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    if !swiped {
        // draw a single point
        drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }

}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}
}

我真的很感激这个问题的一些帮助:)非常感谢提前!

0 个答案:

没有答案