【问题标题】:Change the radius of SKShapeNode改变 SKShapeNode 的半径
【发布时间】:2014-06-20 15:39:24
【问题描述】:

我们如何改变 SKShapeNode 的半径而不重新创建它?在头文件中只有一个带有 circleOfRadius 语法的初始化器。

【问题讨论】:

  • 除了使用 scale 属性没有其他选择。

标签: ios sprite-kit swift


【解决方案1】:

如果您使用路径创建 SKShapeNode,您可以在以后随时更改路径。试试这个:

class GameScene: SKScene {
    override func mouseDown(theEvent: NSEvent) {
        let location = theEvent.locationInNode(self)
        let node = self.nodeAtPoint(location)
        if let circle = node as? SizeableCircle { // clicking on an existing circle
            circle.radius = Double(arc4random_uniform(100))
        } else { // clicking on empty space
            self.addChild(SizeableCircle(radius: 100.0, position: location))
        }
    }
}

class SizeableCircle: SKShapeNode {

    var radius: Double {
        didSet {
            self.path = SizeableCircle.path(self.radius)
        }
    }

    init(radius: Double, position: CGPoint) {
        self.radius = radius

        super.init()

        self.path = SizeableCircle.path(self.radius)
        self.position = position
    }

    class func path(radius: Double) -> CGMutablePathRef {
        var path: CGMutablePathRef = CGPathCreateMutable()
        CGPathAddArc(path, nil, 0.0, 0.0, radius, 0.0, 2.0 * M_PI, true)
        return path
    }

}

这与缩放之间的区别在于,您设置的任何其他内容(例如线宽、文本子节点等)都不会更改,除非您进行设置。

【讨论】:

  • 谢谢,这很有帮助!
【解决方案2】:

斯威夫特 4:

基本上,您可以为scaleToscaleBy 启动SKAction,并以特定的TimeInterval 作为持续时间,无论如何我已经将Grimxn 类翻译成最新的swift,以防您必须使用它其他原因:

class SizeableCircle: SKShapeNode {

    var radius: Double {
        didSet {
            self.path = SizeableCircle.path(radius: self.radius)
        }
    }

    init(radius: Double, position: CGPoint) {
        self.radius = radius

        super.init()

        self.path = SizeableCircle.path(radius: self.radius)
        self.position = position
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    class func path(radius: Double) -> CGMutablePath {
        let path: CGMutablePath = CGMutablePath()
        path.addArc(center: CGPoint.zero, radius: CGFloat(radius), startAngle: 0.0, endAngle: CGFloat(2.0*Double.pi), clockwise: false)
        return path
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多