Update Label Value in Cocos2d

杀马特。学长 韩版系。学妹 提交于 2019-12-22 11:30:49

问题


I am making a game in cocos2d , In that while updating score the old score values get on the label and the new value get overwritten. I m using 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 are not shown and all things get massed up, If any one having idea how to update new score?


回答1:


I don't completely understand what you are doing, because I can't see all of your code. But, I think what you want is this:

In your scene init:

// 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 score changes:

score++ // Not really, but your score changes somehow...
[LblScore setString: [NSString stringWithFormat:@"%d",score]];

This part would probably be in a setScore: or changeScore: method that changes your internal score value and changes the label at the same time.




回答2:


The Solution for my problem is , I must have to define the label declaration in the -(id)init method, there on the value provided from any where there will be no overwriting of the values.

I've tried it and it's working, But still thanks to all who provided me a help



来源:https://stackoverflow.com/questions/2742403/update-label-value-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!