gpt4 book ai didi

swift - 当 2 个手指在屏幕上时应用程序崩溃,触摸开始,触摸移动

转载 作者:搜寻专家 更新时间:2023-11-01 07:15:03 25 4
gpt4 key购买 nike

我不确定发生了什么,所以我无法正确地向您描述它,我制作了一个应用程序,通过用户手指的拖动来绘制一条线,它是一个 sprite 套件游戏,所以我使用了 touchesBegan 和 touchesMoved,所以如果我在画另一条线时将手指放在屏幕上,游戏就会崩溃。我正在寻找的是一种忽略第二次触摸直到第一次触摸结束的方法。我的游戏从触摸的开始位置到触摸结束时的结束位置画一条线这是我的 touches 函数中的代码

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
}

最佳答案

节点只能有一个父节点。您正尝试在场景中多次添加 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 中做同样的事情。当然,我不会深入探讨发生多次触摸时应该发生什么的逻辑。我只是指出错误在哪里以及您的应用崩溃的原因。

关于swift - 当 2 个手指在屏幕上时应用程序崩溃,触摸开始,触摸移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com