我使用的是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];
}
什么是你的日志'的NSLog(@“分数:%d”,得分);'? –
您是否使用ARC并且您的标签属性较弱? –
将断点放入addScore和updateScore:方法并检查它们是否被调用 – Morion