Charls Pico
Charls Pico

Reputation: 91

How to animate an increasing number within an SKLabelNode

How would I go about making the increase of a number within an SKLabelNode animate?

If you have played Clash of Clans you will be familiar with the animation that happens to your totals when you collect either Elixir or Gold.

I.e. when I add 500 to an Integer I want to animate the SKLabelNode to cycle up quickly through the digits from from 0 to 500. So each time I press the button, the animation occurs.

How should I tackle this?

Upvotes: 2

Views: 880

Answers (2)

Seth Rodgers
Seth Rodgers

Reputation: 69

First, print out the score as 0. Initialize a variable i (you don't have to call it that) to 0. Then, in the update function, print out the score and increment i. Worked for me.

override func update(currentTime: NSTimeInterval)
{
   if i <= score
   {
     scoreText.text = "Score \(i)"
     i++
   }
}

Upvotes: 0

ViTUu
ViTUu

Reputation: 1204

I do a quickly code in playground to guide some light, i don't know if the code work 100%, but the logic is this

var timeAnimation = 2;
var coinsLabel:SKLabelNode = SKLabelNode(text: "0");
var gainCoin = 400;
var currentCoin = 20;

func addCoin(coinTotal:Int)
{
    currentCoin = coinTotal+currentCoin;

    self.runAction(SKAction.customActionWithDuration(timeAnimation, updateLabel));

    func updateLabel(node:SKNode!, t:CGFloat) -> Void{
        let coinToAdd = currentCoin*(t/timeAnimation);
        coinsLabel.text = ""+Int(coinToAdd);
    } 
}

Upvotes: 2

Related Questions