在绘制SKShapenodes线时唯一标识它们

时间:2016-11-01 19:08:19

标签: ios sprite-kit swift3

以下代码允许我在屏幕上绘制线条并将它们附加到数组中。我遇到的问题是对行进行唯一命名。忽略我在下面使用(t)但我希望它增加名称(sprite1 .... sprite3),因为我绘制更多的行。任何的想法?我没有故意添加触摸移动的方法。

var lineNode = SKShapeNode()
var lines: [UITouch: SKShapeNode] = [:]
var lineArray:[SKShapeNode] = []    

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

    for t in touches {
        self.touchDown(atPoint: t.location(in: self))
        let location: CGPoint = t.location(in: self)

        // Create a mutable path
        let path: UIBezierPath = UIBezierPath()
        path.move(to: location)

        // Create a shape node using the path
        let lineNode = SKShapeNode(path: path.cgPath)

        //Name the sprite but this is incorrect as t is touches.
        lineNode.name = "sprite\(t)"
        lineNode.strokeColor = SKColor.yellow
        lineNode.glowWidth = 3.0
        self.addChild(lineNode)

        lines[t] = lineNode

        //Add each SKShapenode to Dictionary
        lineArray.append(lineNode)
    }
}

0 个答案:

没有答案