更新Cocos2d中的标签值

V.V*_*V.V 2 objective-c cocos2d-iphone

我正在使用cocos2d进行游戏,因为在更新分数时,旧分数值会出现在标签上并且新值会被覆盖.我使用以下代码来显示分数,

LblScore = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",score]
                         dimensions:CGSizeMake(100, 300) 
                          alignment:UITextAlignmentCenter  
                           fontName:@"Arial" 
                           fontSize:32.0];
Run Code Online (Sandbox Code Playgroud)

因此,如果有人知道如何更新新分数,则不会显示分数值并且所有事情都会被混淆.

Jef*_*f B 8

我不完全理解你在做什么,因为我看不到你的所有代码.但是,我认为你想要的是这个:

在你的场景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)];
Run Code Online (Sandbox Code Playgroud)

当你的分数改变时:

score++ // Not really, but your score changes somehow...
[LblScore setString: [NSString stringWithFormat:@"%d",score]];
Run Code Online (Sandbox Code Playgroud)

这部分可能会在一个setScore:或一个changeScore:方法中改变你的内部得分值并同时改变标签.