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
}
也許你需要一個新的線路實例,當第二次觸摸開始?我真的不知道只是在那裏拋出一些想法。 – Ro4ch
'lineNode'的聲明是什麼? – Grimxn
@Grimxn var lineNode = SKShapeNode() –