I want to just draw a string using SKShapeNode. I use SpriteKit and Swift.
Here is my code:
var line = SKShapeNode()
var ref = CGPathCreateMutable()
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let locationInScene = touch.locationInNode(self)
CGPathMoveToPoint(ref, nil, locationInScene.x, locationInScene.y)
CGPathAddLineToPoint(ref, nil, locationInScene.x, locationInScene.y)
line.path = ref
line.lineWidth = 4
line.fillColor = UIColor.redColor()
line.strokeColor = UIColor.redColor()
self.addChild(line)
}
}
Whenever I run it and try to draw a line, the application crashes with an error: Reason: 'Accepted to add SKNode, which already has a parent: SKShapeNode name:' (null) 'accumulatedFrame: {{0, 0}, {0 , 0}} '
Why is this happening?
source
share