Cocos2d CCLabel update problem for scores
I am using two calls setScore which is going in the init method and UpdateScore when an object is destroyed. When I run the program, I get a crash as soon as UpdateScore is called. Anyone see errors with my code? Thank you very much.
In my .h file I have CCLabel *score; and NSString *text; declared for global use.
-(void)setScore{
scorE = 1;
text = [[NSString alloc] initWithFormat:@"%d", scorE];
score = [CCLabel labelWithString:text fontName:@"Mar开发者_StackOverflow社区ker Felt" fontSize:18];
score.position = ccp(45, 310);
[self addChild: score];
}
-(void)UpdateScore{
scorE++;
NSLog(@"score +1");
[score setString: [NSString stringWithFormat:@"%d",scorE]];
}
It might be possible as you are using class method of CCLabel which is auto-releasing your score object. Try to use options below:
1) score = [[CCLabel labelWithString:text fontName:@"Marker Felt" fontSize:18] retain];
2) score = [[CCLabel alloc] initWithString:text fontName:@"Marker Felt" fontSize:18];
Don't forget to release your score object in your dealloc (or wherever required).
精彩评论