Creating a sprite from a subclass of CCSprite

I have a “Tree” class and it inherits from CCSprite ( class Tree : public cocos2d::CCSprite, public CCTargetedTouchDelegate )

What I want to do is when I create an instance of a “Tree” i want to set some variables and draw the sprite. I tried doing:
Tree * myTree = Tree::create( "tree.png" );

but I get an error saying "Cannot initialize a variable of type ‘Tree ’ with an rvalue of type ’cocos2d::CCSprite
How do I do that?

Hi, Lance

Your “Tree” class doesn’t override the create() function from CCSprite.
And the return type of CCSprite::create() is CCSprite*, so you can’t write this way.

Yeah, I figured that was the case. Instead of creating a class that inherits from CCSprite, I just added a new member variable in my Tree class which holds the Tree sprite. Something like this:

class Tree {

private:
   CCSprite * m_treeSprite;

public:
   void drawTreeSprite( );
   CCSprite * getTreeSprite( );
}

Thanks for the answer.

Yes, that’s a solution.
But I would rather to inherit from CCSprite. Thus you just need to control the “Tree” rather than the “Tree::getTreeSprite()”.
Anyway, just feel free to use what you prefer :slight_smile:

If I want to inherit from CCSprite, how do I go about doing that?
Because I think inheriting from CCSprite is a much faster and “cleaner” way of doing what I want.

I want know better way too.

Like Irving said, I think the best way is to override the CCSprite create() method, then you can add custom parameters.
You should have a look into the sources of CCSprite at cocos2dx\sprite_nodes\CCSprite.h and CCSprite.cpp.

for example, lets suppose we want a Ball object with CCSpriteFrame and color parameters.

Ball.h

class Ball : public CCSprite
{
public:

   // Ball color
   int _m_iColor;

   // returns a pointer to new Ball instance.
   static Ball* create(CCSpriteFrame *pSpriteFrame, int color);

(...)

Ball.cpp

Ball* Ball::create(CCSpriteFrame *pSpriteFrame, int color)
{
   Ball *pSprite = new Ball();
   if (pSprite && pSprite->initWithSpriteFrame(pSpriteFrame))
   {
      // Set to autorelease
      pSprite->autorelease();

      // assign color value
      this->m_iColor = color;

      return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
}

Then to create a instance…

Ball* myBall = Ball::create(MySpriteFrame, 5);

Luis Mendes wrote:

Like Irving said, I think the best way is to override the CCSprite create() method, then you can add custom parameters.
You should have a look into the sources of CCSprite at cocos2dxsprite_nodesCCSprite.h and CCSprite.cpp.
>
for example, lets suppose we want a Ball object with CCSpriteFrame and color parameters.
>
Ball.h
[…]
>
Ball.cpp
[…]
>
Then to create a instance…
[…]

Work for me , thank for help

Thanks, Luis. This works great!

Luis Mendes wrote:

Like Irving said, I think the best way is to override the CCSprite create() method, then you can add custom parameters.
You should have a look into the sources of CCSprite at cocos2dxsprite_nodesCCSprite.h and CCSprite.cpp.
>
for example, lets suppose we want a Ball object with CCSpriteFrame and color parameters.
>
Ball.h
[…]
>
Ball.cpp
[…]
>
Then to create a instance…
[…]

@lumendes I got the error. Do you know why? Thanks

undefined reference to 'Tree::create(int, char const)’*

Thanks @lumendes. That solution was just what I was looking for. :slight_smile: Would it be possible for me to use this solution in a commercial project I’m working on?

@lolyoshi sorry I’m a bit way from cocos2d-x at the moment, the code above was written for 2.X version, maybe you’re using 3.X in which the api changed, everything is namespaced now.

@MDKG sure, go ahead :slight_smile:

@lumendes Yes, I modified a bit and it works well. Thanks :smiley:

For Lumendes’s example, any reason why I am getting invalid use of ‘this’ outside of a non-static member function for the line: this->m_iColor = color;

I’m using v3.3 if that helps. Thanks.