2017-02-18 10 views
0

Im пытается написать код для строки, которая втягивает с сопротивлением пальца, но удаляет, когда палец удаляется (в SpriteKit и Swift 3)Узлы в SKShapeNode появляясь при создании еще один новый узел

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() 
} 

в первая строка рисует и удаляет отлично, но когда я начинаю рисовать вторую строку, первая строка снова появляется.

+0

Возможный дубликат [CGMutablePath не отпуская в стрижа] (http://stackoverflow.com/questions/42320529/cgmutablepath-not-releasing-in-swift) – Losiowaty

+0

Почему ты задайте один и тот же вопрос дважды? – Losiowaty

ответ

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() 
} 
+0

Это действительно сработало, спасибо! –