Swift:重复颜色改变SKSpriteNode

时间:2016-03-03 09:13:48

标签: swift animation skspritenode skaction

我想改变我的SKSpriteNode的颜色,类似于频闪灯,它是如何不断改变颜色然后重复的。我可以用这样的UIView来做到这一点:

UIView.animateWithDuration(2, delay: 0.0, options:[UIViewAnimationOptions.Repeat, UIViewAnimationOptions.Autoreverse], animations: {
self.view.backgroundColor = UIColor.blackColor()
self.view.backgroundColor = UIColor.greenColor()
self.view.backgroundColor = UIColor.grayColor()
self.view.backgroundColor = UIColor.redColor()
}, completion: nil)

但我想对sksprite节点这样做。我做了类似这样的事情并创建了一堆但是它有问题,我觉得可能有更好的方法。

let changeBlue = SKAction.colorizeWithColor(UIColor.blueColor(), colorBlendFactor: 1.0, duration: 2.0)
self.runAction(changeBlue)

let changeRed = SKAction.colorizeWithColor(UIColor.redColor(), colorBlendFactor: 1.0, duration: 2.0)
self.runAction(changeRed)

1 个答案:

答案 0 :(得分:0)

您可能正在寻找sequence(_:)。请参阅SKAction文档中的Creating Actions That Combine or Repeat Other Actions。此外,repeatActionForever(_:)可以无限期地重复操作。

let colorBlue = SKAction.colorizeWithColor(UIColor.blueColor(), colorBlendFactor: 1.0, duration: 2.0)
let colorRed = SKAction.colorizeWithColor(UIColor.redColor(), colorBlendFactor: 1.0, duration: 2.0)
let colorizeSequence = SKAction.sequence([colorBlue, colorRed])
runAction(SKAction.repeatActionForever(colorizeSequence))