Poi*_*pps 4 ios sprite-kit skshapenode swift
我想简单地使用SKShapeNode绘制一条线.我正在使用SpriteKit和Swift.
到目前为止,这是我的代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
每当我运行它并尝试绘制一条线时,应用程序崩溃并出现错误:reason:'尝试添加已经拥有父级的SKNode:SKShapeNode名称:'(null)'accumulationFrame:{{0,0}},{0 ,0}}'
为什么会这样?
那么你一遍又一遍地添加相同的子实例.每次创建行节点并每次将其添加到父节点,然后它将解决您的问题.
var ref = CGPathCreateMutable()
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if let touch = touches.anyObject() as? UITouch {
let location = touch.locationInNode(self)
CGPathMoveToPoint(ref, nil, location.x, location.y)
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let locationInScene = touch.locationInNode(self)
var line = SKShapeNode()
CGPathAddLineToPoint(ref, nil, locationInScene.x, locationInScene.y)
line.path = ref
line.lineWidth = 4
line.fillColor = UIColor.redColor()
line.strokeColor = UIColor.redColor()
self.addChild(line)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4706 次 |
| 最近记录: |