标签文字未更新(cocos2d)

时间:2013-02-11 07:21:53

标签: objective-c cocos2d-iphone

我有一个课程,我打算重复使用多个级别的游戏,我在更新标签文本方面遇到了问题。基本上,我正在尝试将这个类重用于15个级别的游戏。因此,最初标签的值为1,然后在清除级别后应该增加1,然后使用更新的文本重新加载类。这就是我试图更新标签的方式:

GameScene *stage= [stage node];
[[CCDirector sharedDirector]replaceScene:stage];

//stageNo is an integer that I pass to the label as it's text value. As long as its less that 15, it should go inside that code block.
if(stageNo < 15)
{
    stageNo = stageNo + 1;
    [stage.layer.stageLabel setString:[NSString stringWithFormat:@"%i", StageNo]];
}

这只能工作一次所以如果标签的默认值是1,那么在重新加载类之后它就会变为2.之后,它只是坚持2.所以我的问题是,我怎么能每当更新标签文本该类被重新加载以增加1?

2 个答案:

答案 0 :(得分:1)

在init()中将UILabel声明与stringWithFormat分开。它应该工作

答案 1 :(得分:1)

似乎这绝对是一个范围问题。根据你的评论,你做了正确的事情并创建了一个名为stageLabel的属性。唯一的问题是,当你最初设置它时,你没有保留它。 而不是使用

stageLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@", stageNo] fontName:@"Arial" fontSize:18];

你应该使用

self.stageLabel = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@"%@", stageNo] fontName:@"Arial" fontSize:18];