user1597438
user1597438

Reputation: 2221

Label text not updating (cocos2d)

I have a class which I intend to reuse for a game with multiple levels and I'm having a problem with updating the label text. Basically, I'm trying to reuse this class for 15 levels of a game. So initially the value of the label is 1 then it should increase by one after the level has been cleared then the class is reloaded with the updated text. This is how I'm trying to update my label:

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

This only works only once so if the default value of the label is 1, after the class is reloaded it becomes 2. After that, it's just stuck to 2. So my question is, how can I update the label text whenever the class is reloaded to increment by 1?

Upvotes: 0

Views: 343

Answers (2)

T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6383

Seems like this is most definitely a scope problem. According to your comments you've done the right thing and created a property called stageLabel. The only problem is when you set it up initially you are not retaining it. Instead of using

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

you should use

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

Upvotes: 1

isso
isso

Reputation: 9

Separate the UILabel declaration from stringWithFormat in the init(). it then should work

Upvotes: 1

Related Questions