Jerry Zhao
Jerry Zhao

Reputation: 373

SpriteKit SKColor fade in / fade out

Below is a computed property and I've assign this property to backgroundcolor of some SKScene. Now, when the hour changed which means the backgroundColor changed, how do I do could make these color changed with fade-in or fade-out effect? now the background color will changed immediately but that's too straight forward.May be it should be use SKAction? but I cannot find a way.

 static var backgroundColor:SKColor {
                if GameViewController.hour > 6  && GameViewController.hour < 18 {
                    return SKColor.whiteColor()
                }
                if (GameViewController.hour > 16 && GameViewController.hour < 20) || GameViewController.hour > 5 && GameViewController.hour < 8 {
                    return SKColor.grayColor()
                } else { return SKColor.blackColor()}
            }

and here I assign backgroundColor to a refer.

   backgroundColor = GameViewController.backgroundColor 

Upvotes: 1

Views: 368

Answers (1)

MaxKargin
MaxKargin

Reputation: 1545

Yes, use an SKAction to change the color. You need colorizeWithColor:blendFactor:duration:. Specify the color you need, the blend factor (use 1.0 to completely change the color) and duration for how long you need it. It also might be easier to make an SKSpriteNode that you would use for your background. So, for example:

var background: SKSpriteNode = SKSpriteNode(color: color, size: self.frame.size)
var colorize: SKAction = SKAction.colorizeWithColor(color, colorBlendFactor: 1.0, duration: someDurationInSeconds)

Hope this helps

Upvotes: 1

Related Questions