I want to create a simple layer to display some informations, such as a score.
I managed to do like this but I’m not really sure it’s correct :
#import "InformationLayer.h"
#import "GameScene.h"
@implementation InformationLayer
-(void) draw
{
// Complete clear of the screen
[self removeAllChildrenWithCleanup:true];
// Draw my score
[self DrawScore];
}
- (void)DrawScore
{
// create and initialize a label
NSString* l_sScore = [NSString stringWithFormat:@"Score : %d", [[GameScene sharedGameScene] iScore]];
CCLabelTTF* label = [CCLabelTTF labelWithString:l_sScore fontName:@"Marker Felt" fontSize:32];
// get the window (screen) size from CCDirector
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label at the center of the screen
label.position = CGPointMake(size.width - ([label texture].contentSize.width / 2), size.height*2.0/3.0);
// add the label as a child to this Layer
[self addChild:label];
}
@end
I’m dealing with my scenes and layers pretty much like that all the time and I’m really not confortable with clearing all my layer then drawing everything up again. Seems to me that It’s a total overkill ! Still, it does the job…
Since I’m at the bottom of my learning curve, I would like to do right quickly… Could I do something better here ?
It’s completely wrong. Add your nodes only once. Use
drawmethod only if you want to draw something using OpenGL (or cocos2d’s functions likeccDrawLine). You can add your content(labels, sprites, etc.), for example, in the someinitmethod.To change text of your labels use
setString:method.