2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ In what way did creating a class that inherits from CCSprite not work? \$\endgroup\$ Commented Nov 5, 2012 at 21:12

3 Answers 3

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ FANTASTIC ANSWER \$\endgroup\$
    – Coder404
    Commented Dec 28, 2012 at 1:55
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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???

\$\endgroup\$
3
  • \$\begingroup\$ I want to create my own custome object that is a subclass of CCSprite. Could you give an example? \$\endgroup\$
    – Coder404
    Commented Oct 6, 2012 at 17:54
  • \$\begingroup\$ 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]) \$\endgroup\$
    – Sylpheed
    Commented Oct 6, 2012 at 18:05
  • \$\begingroup\$ Ive tried but it has not worked for me. I could really use an example \$\endgroup\$
    – Coder404
    Commented Oct 7, 2012 at 3:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .