Add a new file to your project and use Objective C class as the file type. It will ask you what you want it to inherit from when you create the file, so enter CCSprite.
Your new class will inherit all methods of CCSprite, including the ability to init with an image. In your class's implementation file (the one that ends in .m), create a method called init:
- (id) init {
if ( self = [super init] ) {
// add your custom code here
}
return self;
}
What this is doing is overwriting the CCSprite's init method, which is called during the creation of all CCSprites. Because we are overwriting the CCSprite init method, we must call [super init] to make sure all of the CCSprites init stuff gets done. In this method, we can also add the label you spoke of.
- (id) init {
if ( self = [super init] ) {
CCLabelBMFont *label = [[[CCLabelBMFont alloc] initWithString:@"hello world" fntFile:@"somefont"] autorelease];
[self addChild: label];
}
return self;
}
So we created a label and added it as a child of the sprite. You can of course modify the labels position and all that here as well. Now when you create an object from your custom class, you will of course have to add it to one of your scene layers before it will become visible. Note that adding a child to a node will automatically retain it, so you can create the label as an autorelease object.