2015-10-23 48 views
0

我需要創建一個從屏幕中間以小半徑開始的圓,它從中心逐漸增大。如果圈子是SKSpriteNode,有什麼辦法可以做到這一點?想法是爲了增加半徑,沒有其他品質如中風增加。如何在Swift中更改SKSpriteNode的半徑?

到目前爲止:

func randomCircle(){ 
    let RandomCircleShape = SKShapeNode(circleOfRadius: 1) 
    RandomCircleShape.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) 
    RandomCircleShape.strokeColor = randomColor() 
    RandomCircleShape.lineWidth = 2 
    self.addChild(RandomCircleShape) 
} 

回答

0

下面是實現擴大的循環,保持其線寬的一種方式。爲你的函數添加一個半徑參數並讓它返回SKShapeNode。通過改變半徑隨着時間的推移而增加和刪除在主線程上的節點

var randomCircle: SKShapeNode? 
var currentRadius: CGFloat = 1 

func makeRandomCircle(radius: CGFloat) -> SKShapeNode { 
    let RandomCircleShape = SKShapeNode(circleOfRadius: radius) 
    RandomCircleShape.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) 
    RandomCircleShape.strokeColor = UIColor.redColor() 
    RandomCircleShape.lineWidth = 2 
    return RandomCircleShape 
} 

然後創建擴大的效果:

var i = 0 
while i < 300 { 
    let delay = 0.001 * Double(i) * Double(NSEC_PER_SEC) 
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 
    dispatch_after(time, dispatch_get_main_queue()) { 
     self.currentRadius += 0.25 
     self.randomCircle!.removeFromParent() 
     self.randomCircle = self.makeRandomCircle(self.currentRadius) 
     self.addChild(self.randomCircle!) 
    } 
    i++ 
} 
此外,添加的節點和RADIUS屬性您 GameScene

這段代碼可以放在你想要發生效果的地方。

GIF結果:

enter image description here

相關問題