Met*_*Say 5 ios sprite-kit swift
如何在不重新创建的情况下更改SKShapeNode的半径?在头文件中,只有一个带有circleOfRadius语法的初始化器.
如果使用路径创建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
}
}
Run Code Online (Sandbox Code Playgroud)
这与缩放之间的区别在于,除非您制作它们,否则您设置的任何其他内容(例如行宽,文本子节点等)都不会更改.
| 归档时间: |
|
| 查看次数: |
2064 次 |
| 最近记录: |