2013-02-11 82 views
0

我有一个类,我打算重复使用多个级别的游戏,并且遇到更新标签文本的问题。基本上,我试图重复使用这个游戏的15个级别。所以最初标签的值是1,然后在清除关卡之后它应该增加1,然后重新加载更新后的文本。这是我正在努力更新我的标签:标签文本没有更新(cocos2d)

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?

+0

如何/你在哪里设立stageLabel开始? – 2013-02-11 07:22:56

+0

我在头文件类上设置为CCLabelTTF,然后在init方法中,我将stageNo设置为1,然后我添加了这个:stageLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@“%@”,stageNo] fontName:@“ Arial“fontSize:18]; – user1597438 2013-02-11 07:38:20

+0

听起来像范围问题。如果只在init方法中本地创建stageLabel,则其余代码不知道其存在。尝试使它成为一个伊娃或财产。 – 2013-02-11 07:43:55

回答

1

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

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

的,你应该使用

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

感谢您的建议。我试过这个,我只注意到了一些事情,我的整数在+1之后没有递增。所以它只是停留在2.有什么我在这里失踪? – user1597438 2013-02-11 08:12:27

+0

从您向我们展示的代码中,很难说。实际的if-statment和逻辑似乎是按照它应该做的。问题当然是如何/何时调用此更新的方法,并且在增加它之前可能将某个值重置为某个值? – 2013-02-11 08:15:35

+0

更新方法在阶段被清除时调用,然后它重新加载下一阶段的整个类。我尝试删除我在init方法中为stageNo设置的值,那不是它。 – user1597438 2013-02-11 08:18:09

1

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

+0

我不确定我是否遵循你,但是从我理解的内容来看,这是我所做的: stageNo = stageNo + 1; NSString * strNo = [NSString stringWithFormat:@“%i”,stageNo]; [stage.layer.stageLabel setString:strNo];但这并不起作用 – user1597438 2013-02-11 08:07:22

+0

您需要像这样初始化init()中的stageLabel stageLabel = [[CCLabelTTF alloc] initWithString:[NSString stringWithFormat:@“%@”,stageNo] fontName:@“Arial”fontSize:18] ; – isso 2013-02-11 08:30:14