2017-03-03 80 views
1

我不確定發生了什麼,所以我不能真正地向你描述它,我做了一個應用程序,繪製拖動用戶的手指,它的一個精靈套件遊戲,所以我用touchesBegan和touchesMoved,所以會發生什麼,如果我把一個手指放在屏幕上,而我正在繪製另一條線遊戲崩潰。我正在尋找的是一種忽略第二次觸摸,直到第一次結束的方式。我的遊戲從觸摸的開始位置到結束位置畫一條線,當觸摸結束時 這裏是我觸摸功能中的代碼當兩個手指在屏幕上時,應用程序崩潰,觸摸開始,觸動移動

var lineNode = SKShapeNode() 

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for touch: AnyObject in touches{ 
      positionOfStartTouch = touch.location(in: self) 
      lastPoint = touch.location(in: self) 
      firstPoint = touch.location(in: self) 
     } 
     let pathToDraw = CGMutablePath() 
     print(pathToDraw.isEmpty) 
     pathToDraw.move(to: CGPoint(x: firstPoint.x, y: firstPoint.y)) 
     if frame.width == 375 { 
      lineNode.lineWidth = 4 
     }else if frame.width == 414 { 
      lineNode.lineWidth = 6 
     }else if frame.width == 768 { 
      lineNode.lineWidth = 8 
     } 
     lineNode.strokeColor = UIColor.white 
     lineNode.name = "Line" 
     lineNode.zPosition = 100000 
     lineNode.path = pathToDraw 
     self.addChild(lineNode) 
     shapeNodes.append(lineNode) 
} 
} 
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for touch: AnyObject in touches{ 
      positionInScene = touch.location(in: self) 
     } 
     let pathToDraw = lineNode.path as! CGMutablePath 
     lineNode.removeFromParent() 
     pathToDraw.move(to: CGPoint(x: firstPoint.x, y: firstPoint.y)) 
     pathToDraw.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y)) 
     lineNode.path = pathToDraw 
     shapeNodes.append(lineNode) 
     self.addChild(lineNode) 
     firstPoint = positionInScene 
    } 
+0

也許你需要一個新的線路實例,當第二次觸摸開始?我真的不知道只是在那裏拋出一些想法。 – Ro4ch

+0

'lineNode'的聲明是什麼? – Grimxn

+0

@Grimxn var lineNode = SKShapeNode() –

回答

2

節點只能有一個父節點。您正嘗試在場景中多次添加lineNode。試試這個:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches{ 
     positionInScene = touch.location(in: self) 
    } 
    let pathToDraw = lineNode.path as! CGMutablePath 
    lineNode.removeFromParent() 
    pathToDraw.move(to: CGPoint(x: firstPoint.x, y: firstPoint.y)) 
    pathToDraw.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y)) 
    lineNode.path = pathToDraw 

    if let copy = lineNode.copy() as? SKShapeNode { 
     shapeNodes.append(copy) 
     self.addChild(copy) 
    } 

    firstPoint = positionInScene 

} 

touchesBegan中做同樣的事情。當然,我並沒有深入瞭解在發生多次觸摸時會發生什麼情況。我只是指出錯誤的位置以及您的應用程序崩潰的原因。

相關問題