1

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))

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())

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

Small Legend
  • 733
  • 1
  • 6
  • 20

3 Answers3

4

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))
0x141E
  • 12,613
  • 2
  • 41
  • 54
2

Put this code in your viewDidLoad method and see if it is what you are looking for

    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 this is what you are looking for do something like

    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()
    })
    }`
Colin
  • 198
  • 3
  • 12
0

You have to add your if statements to override func update(currentTime: NSTimeInterval) (this method gets called every frame).

Also, I'd suggest you remove the () after the if statement as they are redundant, the same goes with the self before backgroundColor.

Julia Grill
  • 93
  • 2
  • 9
  • my if statement was inside that function, also thanks for the pointers but your comment hasn't helped my situation – Small Legend Aug 25 '15 at 19:21
  • I tried it in a sample project and it worked for me. What you could do is to track 'score' with breakpoints to see if the problem lies somewhere else. – Julia Grill Aug 25 '15 at 19:30
  • Julia you don't understand my question. The score system is absolutely fine. I'm trying to change the background with an animation, so it fades in basically – Small Legend Aug 25 '15 at 19:34