2012-12-16 67 views
2

我使用的是CCLabelTTF来显示玩家在屏幕上的得分。但是,当我拨打setString来更新分数标签时,它不会更新(因此它始终保持为0)。CCLabelTTF SetString not updating

这里是我的代码:

Player.m,我开始新的PlayerHUD对象:

- (id) init{ 
    if (self = [super init]){ 
     playerHUD = [[PlayerHUD alloc] loadPlayerInterface]; 
     [self addChild:playerHUD z:UPLAYER_Z]; 
    } 
    return self; 
} 

PlayerHUD.m,我开始得分标签:

- (id) loadPlayerInterface{ 
    if (self = [super init]){ 
     score = 0; 
     //Score Label 
     lblScore = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", score] fontName:@"pixel" fontSize:24]; 
     [self addChild:lblScore z:1000]; 
    } 
    return self; 
} 

还在PlayerHUD.m,这里是m Ÿ更新功能:

- (void) updateScore:(NSInteger)_newscore{ 
    score = _newscore; 
    [lblScore setString:[NSString stringWithFormat:@"%d", score]]; 
} 

而且,在Player.m,我所说的更新功能在这里:

- (void) addScore{ 
    int scoreToAdd = 50 

    score += scoreToAdd; 

    NSLog(@"Score:%d", score); 
    [playerHUD updateScore:score]; 
} 
+0

什么是你的日志'的NSLog(@“分数:%d”,得分);'? –

+0

您是否使用ARC并且您的标签属性较弱? –

+0

将断点放入addScore和updateScore:方法并检查它们是否被调用 – Morion

回答

2

好吧,我找到了什么事,我想我会发布在这里,如果有谁遇到这样的:

的问题是,是有原因的,我还是不理,我需要设置一个@property@synthesizeplayerHUD对象,因为,一些操作后,有人becom零,像@InderKumarRathore说。所以设置一个属性并为它合成解决了问题,并且再也不会迷路了!

而且一些研究之后,我认为这是因为cocos2D上v.0.98(一个我之前使用)和Cocos2d V1.0(一个我现在用)内存管理之间的一些修改!

无论如何,谢谢大家的支持,非常感谢!

+0

很好,你已经解决了你的问题。请接受你的回答,以便你的问题关闭。 –

4

我设法用下面的代码来解决这个问题(组字符串为空字符串,然后将其重置您的字符串)

[label setString:@""]; 
[label setString:yourString];