ios - 创建另一个新节点时,SKShapeNode 中的节点重新出现

标签 ios swift sprite-kit

我正在尝试编写一条线的代码,该线通过手指拖动进行绘制,但在手指移开时删除(在 SpriteKitSwift 3 中)

var shapeNodes = [SKShapeNode]()
var pathToDraw = CGMutablePath()
var lineNode = SKShapeNode()

func deleteAllShapeNodes() {

    for node in shapeNodes {
        node.removeFromParent()
    }
    shapeNodes.removeAll(keepingCapacity: false)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        firstPoint = touch.location(in: self)
    }

    shapeNodes.append(lineNode)
    pathToDraw.move(to: CGPoint(x: firstPoint.x, y: firstPoint.y))
    lineNode.lineWidth = 4
    lineNode.strokeColor = UIColor.white
    lineNode.name = "Line"
    lineNode.zPosition = 100000
    lineNode.path = pathToDraw
    self.addChild(lineNode)
}

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

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches{
        TouchEndPosition = touch.location(in: self)
    }
    self.deleteAllShapeNodes()
}

第一条线完美地绘制和删除,但是当我开始绘制第二条线时,第一条线重新出现。

最佳答案

创建一个全新的 CGMutablePath 似乎是获得空 CGMutablePath 的唯一方法。

并且您将 SKShapeNode 的唯一实例(保存在 lineNode 中)添加到 shapeNodes 中,这是没有意义的。您的 lineNode 保留您添加到 pathToDraw 中的所有线条。

通常,您可能需要了解何时实例化对象以及何时释放它们

试试这个:

//### You are adding the same instance multiple times into this array, it's nonsense.
//var shapeNodes = [SKShapeNode]()
//### `pathToDraw` can be kept in the `SKShapeNode`.
//var pathToDraw = CGMutablePath()
//### In this case, this is not a good place to instantiate an `SKShapeNode`.
var lineNode: SKShapeNode?

func deleteAllShapeNodes() {
    //### Release the `SKShapeNode` contained in `lineNode` to delete all lines.
    lineNode?.removeFromParent()
    lineNode = nil
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let firstPoint = touches.first!.location(in: self)

    self.lineNode?.removeFromParent()

    //### If you want somethings which live while dragging, `touchesBegan(_:with:)` is a good place to instantiate them.
    let lineNode = SKShapeNode()
    //### Create an empty new `CGMutablePath` at each `touchesBegan(_:with:)`.
    let pathToDraw = CGMutablePath()

    self.lineNode = lineNode
    pathToDraw.move(to: firstPoint)
    lineNode.lineWidth = 4
    lineNode.strokeColor = UIColor.white
    lineNode.name = "Line"
    lineNode.zPosition = 100000
    //### `pathToDraw` is kept in the `SKShapeNode`.
    lineNode.path = pathToDraw
    self.addChild(lineNode)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let positionInScene = touches.first!.location(in: self)

    if let lineNode = self.lineNode {
        //### Modify the `CGMutablePath` kept in the `SKShapeNode`.
        let pathToDraw = lineNode.path as! CGMutablePath
        pathToDraw.addLine(to: positionInScene)
        //### Refresh the shape of the `SKShapeNode`.
        lineNode.path = pathToDraw
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.deleteAllShapeNodes()
}

关于ios - 创建另一个新节点时,SKShapeNode 中的节点重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42317393/

相关文章:

ios - #define 改变多参数方法名

ios - 动态锥体形状根据障碍物而变化 - 引用 Third Eye Crime 应用程序

ios - 为什么这个 SKPhysicsJointPin 不能将这两个 Sprite 放在一起?

swift - SKNodes 以一致的速度跟随并阻止它们 spazzing?

ios - SQLite.swift:实现表示给定表行的模型类的最佳方式?

ios - 适用于 iOS 的 Apple 记录的低级用户区 API

ios - SpriteKit - Edge 的行为不符合预期

arrays - `removeLast` 会 swift 减少数组的容量吗?

ios - Xcode Firebase 查询以获取特定数据

json - 想查看json元数据