Swift : 背景颜色渐变动画 (SpriteKit)

Swift : Background Color fading animation (SpriteKit)

我正在创建一个游戏,背景颜色一开始是白色的因此:

self.backgroundColor = SKColor.whiteColor()

所以当游戏启动时白色是背景。我有一个评分系统,所以基本上我希望在达到某个分数时颜色会发生变化,因此:

if score < 100{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:3))  
        }
        else if score >= 100 && score < 200{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2.5))
            self.backgroundColor = SKColor.purpleColor()
        }
        else if score >= 200 && score < 300{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2))
            self.backgroundColor = SKColor.greenColor()
        }

但是,这个方法非常笨拙,老实说看起来很糟糕。使用以下方法从场景中移除时,我游戏中的所有内容都是流畅的并且包含淡入淡出:

livesLabel.runAction(SKAction.fadeInWithDuration(0.5))

但我不确定我将如何处理背景颜色。如果我将上面的示例与 backgroundColor 一起使用,例如

self.backgroundColor = SKAction.fadeInWithDuration(SKColor.purpleColor())

我收到错误 "Cannot invoke 'fadeInWithDuration' with an argument list of type '(UIColor)'"

注意:我完全理解尝试将背景颜色分配给动画是愚蠢的。但是我把代码放在那里试图解决我的问题

您必须将 if 语句添加到 override func update(currentTime: NSTimeInterval)(此方法每帧都会调用)。

此外,我建议您删除 if 语句之后的 (),因为它们是多余的,backgroundColor 之前的 self 也是如此。

将此代码放入您的 viewDidLoad 方法中,看看它是否是您要查找的内容

    self.view.backgroundColor = UIColor.blueColor()
    UIView.animateWithDuration(1.0, delay: 1.0, options: nil, animations: { () -> Void in
        self.view.backgroundColor = UIColor.greenColor()
    }, completion: nil)

如果这是您要查找的内容,请执行类似

的操作
    if (score < 100){
        enemy.runAction(SKAction.moveTo(mainBall.position, duration:3))  
    }
    else if (score >= 100 && score < 200){
        enemy.runAction(SKAction.moveTo(mainBall.position, duration:2.5))
        UIView.animateWithDuration(0.5, animations: { () -> Void in
           self.backgroundColor = SKColor.purpleColor()
       })
    }
    else if (score >= 200 && score < 300){
        enemy.runAction(SKAction.moveTo(mainBall.position, duration:2))
         UIView.animateWithDuration(0.5, animations: { () -> Void in
           self.backgroundColor = SKColor.greenColor()
    })
    }`

要从当前背景颜色平滑过渡到其他颜色,请使用 colorizeWithColor SKAction。这是一个例子...

runAction(SKAction.colorizeWithColor(SKColor.purpleColor(), colorBlendFactor: 1.0, duration: 2.0))