Reputation: 6900
Whats the best way to have a label with kills, health or a score that updates as the variables connected with it change? Currently I'm just using a SKLabelNode and assigning text to it with a variable, but the text property is not computed, so it remains static after initialization. I could every time I change the monsteredDestroyed variable, update the text, but I have a feeling there is a better way to do this.
let label = SKLabelNode(fontNamed: "CoolFont")
label.text = "Kills: \(monstersDestroyed)"
Upvotes: 1
Views: 1781
Reputation: 321
A better way to do this is maybe using property observers.
var shots: Int = 0 {
didSet {
shotsLabel.text = "\(shots)"
}
This way, whenever you set the shots value, the label text will change automatically.
EDIT: I forgot to mention that you should check first if the shotsLabel is not nil, because you can set the shots variable before the view loads.
Upvotes: 1
Reputation: 20284
There is not better way to do this. You cannot make a SKLabelNode 'listen' for changes on a variable (actually there is a system called Key-Value Observing, but in this case it would be overkill). I would suggest setting up a method which would manage the score as well as update it on the screen. It would be a simple matter of calling that function to update as well as keep track of the score, or any other variable which is to be displayed on the screen.
let lblScore = SKLabelNode() //Label with global scope.
var score:Int = 0 //The variable holding the score.
func updateScoreWithValue (value: Int) {
score += value
lblScore.setText("\(score)")
}
You can simply call this method with the value to increment/decrement the score.
self.updateScoreWithValue(10) //Adds 10 and updates it on screen.
self.updateScoreWithValue(-10) //Subtracts 10.
Upvotes: 4
Reputation: 7341
As ZeMoon mentioned, you can always use something like KVO to update the value. Myself, I prefer a bit more explicit control on when the changes are made and hence will set the values myself versus KVO. What I typically do is let all the values aggregate and then have a HUD manager figure out if values are dirty and then update values accordingly. In other words, the HUD manager is more or less a "view controller", bridging items like score, health, etc to the view, which is your node tree.
Also note that I'd typically wrap my SKLabelNode into another object which hides the implementation. This allows me to still have a text property, but if I want to, I can change how I'm actually doing the text.
Upvotes: 1
Reputation: 19922
I could every time I change the monsteredDestroyed variable, update the text, but I have a feeling there is a better way to do this.
There's no better way than that, you're on the right track.
Upvotes: 1