in the header file:
@interface GameLayer : CCLayer { CCLabelTTF *_scoreLabel; } -(void) updateScore:(int) newScore;
in the implementation file:
-(id) init { if( (self=[super init])) { // .. // add score label _scoreLabel = [CCLabelTTF labelWithString:@"0" dimensions:CGSizeMake(200,30) alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:30]; [self addChild:_scoreLabel]; _scoreLabel.position = ccp( screenSize.width-100, screenSize.height-20); } return self; } -(void) updateScore:(int) newScore { [_scoreLabel setString: [NSString stringWithFormat:@"%d", newScore]]; }
EDIT : if you do not want to use ivar, you can use tags:
[self addChild:scoreLabel z:0 tag:kScoreLabel]; // ... CCLabelTTF *scoreLabel = (CCLabelTTF*)[self getChildByTag:kScoreLabel];
EDIT 2 . For performance reasons, you should switch to CCLabelAtlas or CCBitmapFontAtlas if you update your account very often.
Also read cocos2d shortcuts programming guide .
Felix
source share