Swift:背景颜色渐变动画(SpriteKit) [英] Swift : Background Color fading animation (SpriteKit)
问题描述
我正在创建一个游戏,因此背景颜色为白色:
I'm creating a game, the background color is white to begin with hence:
self.backgroundColor = SKColor.whiteColor()
因此,当游戏启动白色时是背景。我有一个评分系统,所以基本上我希望颜色在达到某个分数时改变:
So when the game initiates white is the background. I have a scoring system, so essentially I want the colors to change when a certain score is reached hence:
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()
}
然而,这种方法非常笨重,如果我诚实的话,看起来非常糟糕。我的游戏中的所有内容都是流畅的,并且在使用以下方式从场景中删除时包含淡入淡出:
however, this method is very clunky and looks pretty awful if I'm honest. Everything in my game is fluid and contains fades when being removed from the scene using:
livesLabel.runAction(SKAction.fadeInWithDuration(0.5))
但我不确定如何使用背景颜色进行此操作。如果我在backgroundColor中使用上面的例子,例如
But I am unsure as to how I would proceed to do this with the background color. If I use the above example with backgroundColor such as
self.backgroundColor = SKAction.fadeInWithDuration(SKColor.purpleColor())
我收到错误无法使用'(UIColor)'的参数列表调用'fadeInWithDuration'
I get the error "Cannot invoke 'fadeInWithDuration' with an argument list of type '(UIColor)'"
注意:我完全理解尝试将背景颜色分配给动画是愚蠢的。但我把那些代码放在那里尝试解决我的问题
Note: I understand completely that attempting to assign the background color to an animation is silly. But I put that code in there to try get my problem across
推荐答案
从当前背景色平滑过渡到另一种颜色,使用 colorizeWithColor
SKAction
。这是一个例子...
To smoothly transition from the current background color to a different color, use the colorizeWithColor
SKAction
. Here's an example...
runAction(SKAction.colorizeWithColor(SKColor.purpleColor(), colorBlendFactor: 1.0, duration: 2.0))
这篇关于Swift:背景颜色渐变动画(SpriteKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!