objective-c - CCLabelTTF SetString 未更新

标签 objective-c ios cocoa-touch cocos2d-iphone cclabelttf

我正在使用 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/

相关文章:

iPhone如何实现一个 "Wide"的UITableViewCell?

ios - 如何将单元格插入 UITableViewController

xcode - 在方案选项中为应用程序语言添加更多选项

iphone - 如何在 iPhone 上将 UIWebView 的内容居中

objective-c - 解析pdf文件并转换

objective-c - 如何将 com.apple.systemuiserver 首选项重新加载到 SystemUIServer 应用程序中?

ios - xcode导航标题以编程方式编写

ios - 在两种方法中调用时,在控制中心中快速更新媒体元数据不起作用

iphone - 发布到iTunes的iOS存档与项目不同

ios - 对 xxx 的引用不明确