iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用SKShapeNode绘制一条线大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想简单地使用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,LOCATIOnInScene.y)
        line.path = ref
        line.lineWidth = 4
        line.fillColor = UIColor.redColor()
        line.strokeColor = UIColor.redColor()
        self.addChild(linE)

    }
}

每当我运行它并尝试绘制一条线时,应用程序崩溃并出现错误
 原因:’尝试添加已有父级的SKNode:SKShapeNode@L_197_5@:'(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,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,LOCATIOnInScene.y)
        line.path = ref
        line.lineWidth = 4
        line.fillColor = UIColor.redColor()
        line.strokeColor = UIColor.redColor()
        self.addChild(linE)
    }
}

大佬总结

以上是大佬教程为你收集整理的ios – 使用SKShapeNode绘制一条线全部内容,希望文章能够帮你解决ios – 使用SKShapeNode绘制一条线所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。