Update label value in Cocos2d
I am making a game in cocos2d, while when the score is updated, the old score values get hit the label and the new value gets overwritten. I am using the following code to display the score,
LblScore = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",score]
dimensions:CGSizeMake(100, 300)
alignment:UITextAlignmentCenter
fontName:@"Arial"
fontSize:32.0];
Because of this, the score value is not displayed and all things are collected, if anyone has an idea how to update the new score?
+2
a source to share
2 answers
I don't quite understand what you are doing because I cannot see all of your code. But I think what you want is:
In your init scene:
// Both of these are class variables
score = 0;
LblScore = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",score] dimensions:CGSizeMake(100, 300) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:32.0];
// Position the score, wherever you want it
[LblScore setPosition: CGPointMake(300, 240)];
When your account changes:
score++ // Not really, but your score changes somehow...
[LblScore setString: [NSString stringWithFormat:@"%d",score]];
This part will probably be in a method setScore:
or changeScore:
that will change your internal score value and change the label at the same time.
+8
a source to share