3

I wrote a function called updateLabel to count up my collected coins over time. I call that function using a NSTimer to get the counting up effect.

Everything works great using iOS 8 but as soon as I call the function using iOS 7.1 my Label just turns blank... If I print out the Label.text I get the right number but it just does not show. Even the sound "coinSound" works.

scoreLabel = SKLabelNode()
scoreLabel.text = "0"
self.addChild(scoreLabel)           // shows up and works with "0"

self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self.self, selector: "updateLabel", userInfo: nil, repeats: true)



func updateLabel() {

    var score:Int = coinsCollected
    var current:Int = self.scoreLabel.text.toInt()!

    if (current < score) {

        current += 1
        scoreLabel.text = String(current)
        println("current: \(current) , labelText: \(scoreLabel.text)")    
        self.runAction(coinSound)                                         
    } else {
        timer.invalidate()
    }

}

Thanks for your help

MikeB
  • 1,619
  • 2
  • 15
  • 27

1 Answers1

0

Does the label ever show, or does it only go blank when you try to update it?

If it's the former case, then it might be that the label is not showing at all, not just that it's not updating the text. For instance, it could be behind some other element when you don't expect it, it could be being rendered somewhere you don't expect, it could be being rendered with a zero alpha or in a color that matches the background (white on white, for instance), or it could be unable to find the information it needs to create the glyphs (i.e., you may be using a font that is not available on that system).

If it's the latter case, it could be a problem with how you're computing the text. I notice that you're doing something a little odd - you're getting the "current" value by taking the Int value of the current text, adding one, and then setting the text back. The problem with that approach is that if you ever have a non-integer value as text in that field, then you will forever break that field. A better approach would be to keep an Int variable, increment it, and then set the field's text to that.

You could test between these cases by just setting that field's text to simply "1" or something to see if that shows up. If you can get the static text member to display, then you can move to the dynamic display. But if the static text is not showing, then that means the timer-based dynamic code had nothing to do with the problem.

One thing to check is that you're using a valid font name. "Arial" is not a font name, for instance - you'd have to use "ArialMT". Be sure you're using the actual font name.

cc.
  • 3,041
  • 1
  • 21
  • 24