gpt4 book ai didi

objective-c - CCLabelTTF SetString 未更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:45 25 4
gpt4 key购买 nike

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

这是我的代码:

Player.m 中,我启动了一个新的 PlayerHUD 对象:

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

PlayerHUD.m 中,我启动了 Score Label:

- (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 中,这是我的更新函数:

- (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];
}

最佳答案

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

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

关于objective-c - CCLabelTTF SetString 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com