Ios 将一条直线从Sprite拖动到SpriteKit中的手指位置

Ios 将一条直线从Sprite拖动到SpriteKit中的手指位置,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我想知道问题是否与节点的排列有关,而不是与代码有关,但我似乎无法使这一行正常工作。我想画一条从触摸节点到触摸位置的直线。然后,节点将移动到触摸结束的位置。我的代码基于我在这里看到的一些内容,但仍然无法显示出来。此相关节点是SKSpriteNode的子类,此代码位于该类中,而不是主游戏场景类中 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { self.childNode(wi

我想知道问题是否与节点的排列有关,而不是与代码有关,但我似乎无法使这一行正常工作。我想画一条从触摸节点到触摸位置的直线。然后,节点将移动到触摸结束的位置。我的代码基于我在这里看到的一些内容,但仍然无法显示出来。此相关节点是
SKSpriteNode
的子类,此代码位于该类中,而不是主
游戏场景
类中

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.childNode(withName: "line")?.removeFromParent()  // Remove any previous line
    guard let touch = touches.first else { return } 
    let positionInScene = touch.location(in: self.parent!)  // Use 'parent' here? 
    let path = CGMutablePath()
    let line = SKShapeNode(path: path)  // Create shape node with path as path
    line.name = "line"
    line.zPosition = 5000  // Just trying to make sure it's on top
    line.strokeColor = UIColor.red
    line.lineWidth = 20
    line.fillColor = UIColor.red
    path.move(to: CGPoint(x: self.position.x, y: self.position.y))
    path.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y))
    self.addChild(line)
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
self.childNode(名称为:“line”)?.removeFromParent()//删除前面的任何一行
guard let touch=touch.first else{return}
让positionInScene=touch.location(在:self.parent!)//在这里使用“parent”吗?
let path=CGMutablePath()
让line=SKShapeNode(path:path)//使用path作为path创建形状节点
line.name=“line”
line.zPosition=5000//只是想确保它在上面
line.strokeColor=UIColor.red
line.lineWidth=20
line.fillColor=UIColor.red
移动路径(到:CGPoint(x:self.position.x,y:self.position.y))
路径添加线(到:CGPoint(x:positionInScene.x,y:positionInScene.y))
self.addChild(行)
}

当我在
touchesbreated
中创建并添加路径时,它会显示出来,但这会画出一堆线,我只需要一条

简单的错误。只需在绘制线后为其指定路径即可

let path = CGMutablePath()    
path.move(to: CGPoint(x: self.position.x, y: self.position.y))
path.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y))

let line = SKShapeNode(path: path)  // Create shape node with path as path
self.addChild(line)