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


The solution to my problem is that I have to define a label declaration in the init () method, where the value is specified from anywhere, where there will be no value rewriting.



I tried and it works, But still thanks to everyone who helped me

0


a source


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







All Articles