Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been pulling my hair out trying to make an object in cocos 2d that is a CCSprite (with an image) and a CCLabelBMFont.

I tried making a CCNode object and I tried making a custom CCSprite object but none of it worked!

Can someone show me how to do this?

Thanks.

share|improve this question
    
In what way did creating a class that inherits from CCSprite not work? –  doppelgreener Nov 5 '12 at 21:12

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
FANTASTIC ANSWER –  Coder404 Dec 28 '12 at 1:55

In order to use OOP in Cocos2d, you can utilize the following library

For exact solution utilize the learning

And for a nice introduction, please read this

share|improve this answer

Subclassing a CCNode and adding CCSprite and CCLabelBMFont as its properties will do the job. You can do this manually (by adding a CCLabelBMFont on your CCSprite) but if you're gonna use this across your project then this is way more efficient.

I don't get your main question though. Cocos2d is already in OOP. Maybe you're talking about MVC???

share|improve this answer
    
I want to create my own custome object that is a subclass of CCSprite. Could you give an example? –  Coder404 Oct 6 '12 at 17:54
    
I'm currenlty not on the mac so I won't be able to give any code. I think you've already answered your question. Just create a new class that inherits from CCSprite (eg. CCLabeledSprite). In that class, just add ONE property => CCLabelBMFont and everytime you set that it gets added to that custom class as CCNode. You should override the setter in this case. (eg. [self addChild:_label]) –  Sylpheed Oct 6 '12 at 18:05
    
Ive tried but it has not worked for me. I could really use an example –  Coder404 Oct 7 '12 at 3:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.