Reanimation Reanimation - 7 months ago 17
Swift Question

An optimal way to Fade-In and Fade-Out SKLabelNodes

I am working on a small SpriteKit game.

I have a "Tips" section on the Home Screen that I want to pulse in and out, each time displaying different Tips.

I have a method that works, which I wrote myself, but it's messy and I'm sure there's an better way it could be done. I was hoping someone could show me a way that maybe I missed (or went a long way around doing).

This is how I currently do it:

func createTipsLabels(){
//create SKLabelNodes
//add properties to Labels

//tip1Label... etc
//tip2Label... etc
//tip3Label... etc

//now animate (or pulse) in tips label, one at a time...
let tSeq = SKAction.sequence([
SKAction.runBlock(self.fadeTip1In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip1Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip2In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip2Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip3In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip3Out),
SKAction.waitForDuration(2),
])

runAction(SKAction.repeatActionForever(tSeq)) //...the repeat forever

}

//put in separate methods to allow to be called in runBlocks above
func fadeTip1In() { tip1Label.alpha = 0; tip1Label.runAction(SKAction.fadeInWithDuration(1)) ; print("1") }
func fadeTip1Out(){ tip1Label.alpha = 1; tip1Label.runAction(SKAction.fadeOutWithDuration(1)); print("2") }
func fadeTip2In() { tip2Label.alpha = 0; tip2Label.runAction(SKAction.fadeInWithDuration(1)) ; print("3") }
func fadeTip2Out(){ tip2Label.alpha = 1; tip2Label.runAction(SKAction.fadeOutWithDuration(1)); print("4") }
func fadeTip3In() { tip3Label.alpha = 0; tip3Label.runAction(SKAction.fadeInWithDuration(1)) ; print("5") }
func fadeTip3Out(){ tip3Label.alpha = 1; tip3Label.runAction(SKAction.fadeOutWithDuration(1)); print("6") }


How can I optimise this?

Answer

There is no need to create multiple labels nor do multiple actions, just create an array of what you want to do, and iterate through it.

func createTipsLabels()
{
   let tips = ["1","2","3","4","5"];
   var tipCounter = 0
   {
     didSet
     {
       if (tipCounter >= tips.count)
       {
         tipCounter = 0;
       }
     }
   }
   tipLabel.alpha = 0;
   let tSeq = SKAction.sequence([

        SKAction.runBlock({[unowned self] in self.tipLabel.text = tips[tipCounter]; print(tips[tipCounter]); tipCounter+=1;}),
        SKAction.fadeInWithDuration(1),
        SKAction.waitForDuration(5),
        SKAction.fadeOutWithDuration(1),

        SKAction.waitForDuration(2)

    ])


    tipLabel.runAction(SKAction.repeatActionForever(tSeq))  //...the repeat forever

}