iosswiftsklabelnode

IOS Swift SpriteKit SKLabel, animate appearance of text


I want to to animate appearance of Label's text so that it looks like it is being typed in at the moment. I was wondering if there is a relatively easy way to do so in SWIFT. Thank you.


Solution

  • Try this:

    class GameScene : SKScene {
    
        let text = ["G", "a", "m", "e"]
    
        var labelText = ""
    
        let labelNode = SKLabelNode()
    
        var calls : Int = 0
    
        var timer : NSTimer!
    
        override func didMoveToView(view: SKView) {
    
            timer = NSTimer.scheduledTimerWithTimeInterval(YOUR_DESIRED_INTERVAL, target: self, selector: #selector(self.updateLabelText), userInfo: nil, repeats: true)
    
            labelNode.text = labelText
            self.addChild(labelNode)
        }
    
    
        func updateLabelText() {
    
            labelText += text[calls]
            labelNode.text = labelText
            calls += 1
    
            if calls == text.count + 1 {
                timer.invalidate()
        }
    }