Search code examples
ios8sprite-kitsklabelnode

SKLabelNode disappears when text is changed


I am trying to create a sklabelnode that displays the score of a sprite kit game. The text displays 0 initially, but disappears when the text is changed. The node is declared globally after the class declaration along with the score, that's an int. Here's the code:

var scoreLabel = SKLabelNode(fontNamed: "Chalkduster")

    override func didMoveToView(view: SKView) {
    scoreLabel.text = String(score)
    scoreLabel.fontSize = 48
    scoreLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMaxY(self.frame)-50)

    self.addChild(scoreLabel)
}
func updateScoreAndLabel()->(scoreint:Int,label:SKLabelNode){
    score++
    scoreLabel.text = String(score)
    return (score, scoreLabel)
}

updateScoreAndLabel() is called from another function that isn't important for this question so I did not include it. The problem is not in the function call I believe.


Solution

  • Just putting this here as an official answer in case someone else comes across it, and since it's the accepted answer by the OP.

    Try setting the zPosition property of the SKLabelNode to a high value. Depending on when you've added it in the view hierarchy, it may be present, but behind your other nodes.