Coder1000 Coder1000 - 3 months ago 42
Swift Question

How to gradually (animated) change background color in Spritekit?

SOLUTIONS I TRIED:

Gradually change background color in Swift

Swift : Background Color fading animation (SpriteKit)




WHAT I CURRENTLY HAVE (not working):

class GameScene: SKScene, SKPhysicsContactDelegate{

var background = SKSpriteNode()

var colorizeToRed = SKAction()


override func didMoveToView(view: SKView) {

background = SKSpriteNode(color: UIColor.blueColor(), size: self.frame.size)

background.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)

background.zPosition = 15

colorizeToRed = SKAction.colorizeWithColor(SKColor(red: 90, green: 0, blue: 0, alpha: 1), colorBlendFactor: 1.0, duration: 2.0)

background.runAction(colorizeToRed)

}

}





MY PROBLEM:

I don't see the background change color. The only time I see it change color is if I set it directly with no animation using
self.backgroundColor = UIColor().aColor
.

EDIT: In fact, I dont'even see the blue background... Something is wrong with my SKSpriteNode, but what ?




QUESTION:

How to gradually (animated) change background color in Spritekit ?




BEFORE YOU ASK:

1) YES, I have already tried changing the zPosition in case the SKNode was hidden.

2) YES, I have checked the doc for
SKAction.colorizeWithColor()
.

3) YES, I am almost certain the solution to my problem is very simple (probably some mistake on my part), but what ?

4) YES, I am open to any suggestions telling me the error is somewhere else in my code, but only if you precise where that error might be.

5) NO, I will not post my ENTIRE code here, please ask for specific parts if necessary.




Thank you in advance for any help you provide ^^ !

Nik Nik
Answer

By the looks of it, I think you just forgot to add the background to the scene. Try this:

addChild(background)

If that doesn't help, read on...

If you're just fading in an SKSpriteNode then you can use he SKAction.fadeIn to fade the node in. Just set its alpha to 0 to start and fade it in by doing something like this:

background.run(SKAction.fadeInWithDuration(2))