Anton O.
Anton O.

Reputation: 663

Animate a number changing in SpriteKit

I'm using Swift and SpriteKit. I want a number to change from 20 to 10. I want while its changing to display the changing of the number. To be more specific, I want 19 to show for 0.1 seconds, 18 to show for 0.1 seconds, and so on.

Upvotes: 0

Views: 921

Answers (2)

Anton O.
Anton O.

Reputation: 663

I found an answer:

var counterNumber = 20
var counterLabel = SKLabelNode()

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    counterLabel.text = "\(counterNumber)"
    counterLabel.fontName = "Helvetica Neue"
    counterLabel.fontSize = 100
    counterLabel.fontColor = SKColor.blackColor()
    counterLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    self.addChild(counterLabel)


}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    let wait = SKAction.waitForDuration(0.5)
    let block = SKAction.runBlock({

        self.counterNumber = self.counterNumber - 1
        self.counterLabel.text = "\(self.counterNumber)"

    })

    let sequence = SKAction.sequence([wait, block])

    counterLabel.runAction(SKAction.repeatAction(sequence, count: 10))


}

Upvotes: 1

Chris Slowik
Chris Slowik

Reputation: 2879

You can use the update function in your SKScene. It has one parameter - the current time. Using that (and maybe a start time set when your scene loads), you can know how much time has passed in your scene. Then its just a simple matter of updating your label, as you've already shown.

Upvotes: 1

Related Questions