0

I am programming a game, and using a SKLabel to view the current score. The problem is that when score changes it doesn't display the change in the screen at the moment, it does after a second more or less. What can I do to see the changes in the moment I use [sklabelscore setTextScore:++self.score]. Can I force render or something similar?

I call the setTextScore when the user touches an enemy, with touchesBegan:withEvent:

setTextScore: implementation is

SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
  • Hi Turtle, is it possible that you are just incrementing the score incorrectly? It looks like you are might be assigning the current value of score to the label, and then incrementing it. Does changing ++score to score++ solve your problem – nacross Feb 20 '14 at 08:46
  • ++score increments first, and then calls the funcion (tested in debugging to be sure) –  Feb 20 '14 at 08:49
  • Okay no worries, I didn't check which way around the assignment verse increment worked myself. – nacross Feb 20 '14 at 08:51
  • I haven't seen a method called setTextScore before on SKLabelNode, is this your own method? I am only aware of text property but it takes a string not number primitive. – nacross Feb 20 '14 at 09:07
  • yeah is my metod, converts the int in NSString and sets the sklabelnode.text –  Feb 20 '14 at 09:08
  • 1
    At what point do u call [sklabelscore setTextScore:++score]? – ZeMoon Feb 20 '14 at 09:17
  • when the user touches an enemy in the screen ( touchesBegan:withEvent: ) –  Feb 20 '14 at 09:23
  • Do you experience a similar delay if you update the label from else where in your code? I just added some tests labels to a project I am working on and things update immediately. Is your game otherwise responsive with a good framerate? – nacross Feb 20 '14 at 09:30
  • the game is very simple, is always at 60 fps and 7 nodes max :S I didn't test the label outside the touchesBegan function –  Feb 20 '14 at 09:39

2 Answers2

2

It seems most likely that the ++score you increment is a local variable and not the same as self.score.

You call the method as follows:

[sklabelscore setTextScore:++score]

Which means its signature with code must be:

-(void) setTextScore:(int)theNewScore
{
    SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
    scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
}

So you're passing in theNewScore but instead of using that in the format string you use self.score which may never be updated if the incremented ++score variable is a local variable (ie never assigns its new value to self.score).

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • sorry, my mistake, the value I pass to the function is self.score, not score :S editing question –  Feb 20 '14 at 09:43
  • 1
    Then it begs the question why are you passing in self.score as parameter of the method when self.score already is readily available (it's a property/ivar)? How is self.score defined? Post the header and the entire setTextScore: method. – CodeSmile Feb 20 '14 at 09:47
1

Solved... I feel like an idiot :S

The problem was I fade out the enemy when I touched it, and then, after 0.5 seconds, changes the label. I put that out of the block and all working fine.

Changed setTextScore: method because was redundant (thanks @LearnCocos2D)

...
SKAction* fade=[SKAction fadeOutWithDuration:0.5];
[node runAction:fade completion:^{
    [node removeFromParent];
    self.enemyNumber--;
    self.score++;
    SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
    scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
}];

The new form (outside of the block):

...
self.score++;
SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
SKAction* fade=[SKAction fadeOutWithDuration:0.5];
[node runAction:fade completion:^{
    [node removeFromParent];
}];

Thanks for your help and sorry for asking this stupid question...

  • 1
    So if @LearnCocos2D helped you, why haven't you upvoted his answer or accepted it? – trojanfoe Feb 20 '14 at 12:16
  • because his solution didn't helped solving the problem, the problem was other. But I appreciate his help, –  Feb 20 '14 at 17:13