【发布时间】:2021-12-08 16:58:54
【问题描述】:
在我触摸某个精灵对象后,我正试图让一条直线跟随我的手指。到目前为止,我一直在工作,除了绘制一条线,而是在触摸之后绘制了无限的线......
我的代码:
import SpriteKit
import GameplayKit
class WireDrawTest: SKScene{
var drawingLayer: CAShapeLayer!
var redBox = SKSpriteNode()
var redBoxPoint = CGPoint(x: 445, y: 800)
var redBoxTouched:Int = -1
var currentTouch = touchesMoved
func drawLine(from: CGPoint, to: CGPoint) {
let line = SKShapeNode()
let path = CGMutablePath()
path.addLines(between: [from, to])
line.path = path
line.strokeColor = UIColor.yellow
line.lineWidth = 13
addChild(line)
}
override func didMove(to view: SKView) {
redBox = self.childNode(withName: "redBox") as! SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print(redBoxTouched)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "redBox" {
if redBoxTouched == -1 {
redBoxTouched = 1
}
}
if redBoxTouched == 1 {
drawLine(from: redBoxPoint, to: location)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print(redBoxTouched)
if redBoxTouched == 1 {
redBoxTouched = -1
}
}
}
这是当前结果的截图: screenshot
TIA!!
【问题讨论】:
-
"而不是画一条线" 从什么点到什么点?如果你只是想画一条直线,不就是将
touchesBegan的一个点连接到touchesEnded的另一个点吗? -
我不确定。我是新来的快速大声笑。但我并不想在触摸结束后保留“绘图”。我希望这条线在 touchesEnded 时消失,但这部分不是我正在努力解决的问题。当您触摸屏幕时,我希望一条线跟随您的手指,而不是连续打印线条。
标签: swift xcode sprite-kit skshapenode