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?
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( );
}
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
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.
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;
}
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…
[…]
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…
[…]
Thanks @lumendes. That solution was just what I was looking for. 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.
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;