2013-02-11 84 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