Swift : Background Color fading animation (SpriteKit)

前端 未结 3 1491
余生分开走
余生分开走 2021-01-07 10:42

I\'m creating a game, the background color is white to begin with hence:

self.backgroundColor = SKColor.whiteColor()

So when the game initi

相关标签:
3条回答
  • 2021-01-07 11:22

    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))
    
    0 讨论(0)
  • 2021-01-07 11:24

    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.

    0 讨论(0)
  • 2021-01-07 11:29

    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()
        })
        }`
    
    0 讨论(0)
提交回复
热议问题