I have implemented the CCLabelProtocol and CCRGBAProtocol with a class which I named Label:
#import <cocos2d.h>
@interface Label : CCNode <CCLabelProtocol, CCRGBAProtocol>
@property (nonatomic, copy, readwrite) NSString* string;
@property (nonatomic, readwrite) ccColor3B color;
@property (nonatomic, readwrite) GLubyte opacity;
- (id) initWithString: (NSString*) aString;
@end
Since it comes handy I also made a initWithString method:
#import "Label.h"
@implementation Label
@synthesize string,opacity,color;
- (id) initWithString: (NSString*) aString
{
if(self=[super init])
{
string= aString;
color.r=255;
color.g=0;
color.b=0;
opacity= 10;
}
return self;
}
- (ccColor3B) color
{
return color;
}
@end
Then in the HelloWorldLayer’s init method I do this:
Label* label= [[Label alloc]initWithString: @"Start"];
CCMenuItemLabel* item= [CCMenuItemLabel itemWithLabel: label block:^(id sender)
{
NSLog(@"Label Clicked");
}];
CCMenu* menu= [CCMenu menuWithItems: item, nil];
[self addChild: menu];
I am using the a plain cocos2d template, so this is inside a CCLayer class.
The screen is black, I don’t get the label to appear as I want.
That’s because you merely adopted some protocols, but subclassed from CCNode. CCNode is a non-rendering (invisible) node.
If you want a label, subclass from CCLabelTTF and omit the protocols since CCLabelTTF already implements them.
However it’s overkill to create a CCLabelTTF subclass unless you want to change how the label behaves or renders. If all you need to change is text and color, use the CCLabelTTF initializers and its color property.