Question

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];
Was it helpful?

Solution 2

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...

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top