【问题标题】:App crash when 2 fingers are on the screen, touches began, touches moved当两根手指在屏幕上时,应用程序崩溃,开始触摸,触摸移动
【发布时间】:2017-03-03 08:32:06
【问题描述】:

我不确定发生了什么,所以我无法正确地向你描述它,我制作了一个应用程序,它通过用户手指的拖动来绘制一条线,它是一个精灵套件游戏,所以我使用了 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
    }

【问题讨论】:

  • 也许您在第二次触摸开始时需要一个新的线条实例?我真的不知道只是把想法扔在那里。
  • lineNode的声明是什么?
  • @Grimxn var lineNode = SKShapeNode()
  • 管理状态以忽略辅助触摸...使用布尔值忽略辅助触摸,并保留初始触摸的副本以通过开始/移动/结束调用来管理布尔值。
  • 首先,添加堆栈跟踪,或者至少添加错误消息。接下来,当您已经发布了这么多代码时,请始终尝试制作一个可复制粘贴的工作示例。关于问题...查看我的答案。

标签: swift sprite-kit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-12-28
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多