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

时间:2017-02-18 16:00:43

标签: 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()
}

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

1 个答案:

答案 0 :(得分:0)

创建一个全新的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()
}