Extending Sprite Class

Hi

I have a class named MySprite which inherent Sprite and adds a bunch of properties to it.

Created a Factory class called createMySprite which calls
MySprite* theSprite = MySprite::create(“image.png”);

so I can continue to initialize theSprite.
I receive an error: *a value of type "cocos2d::Sprite * cannot be used to initialize an entity of type "MySprite "

What am I doing wrong ?

Show us MySprite class

It’s showing you what the problem is.

You haven’t implemented the MySprite::create method, and you’re still using the Sprite::create, which returns an instance of type Sprite, not MySprite. You need to implement your own MySprite::create method, something like this (you can just copy the Sprite::create and modify it):

MySprite* MySprite::create()
{
    auto* sprite = new (std::nothrow) MySprite();
    if (sprite && sprite->init())
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

Thanks, but this does not make sense.
I want to use the parent (Sprite) method of create that receive an string of an image file.

The parent method return a Sprite, I know, but MySprite inherent it (having it is : public Sprite).
So why doesnt it work ?

I see that this is more of a c++ question and not Cocos related.
I’m coming from Java, so maybe things works different here.
Could you explain ?

Thanks

Post more code. So we can see exactly what you are doing.

Here is the Header file

class MySprite : public Sprite
{
	void doStuffForMySprite();
	
public:

	MySprite* createMySprite();
};

Here is the CPP file
#include “MySprite.h”

MySprite* MySprite::createMySprite()
{
	MySprite* newSprite = MySprite::create("mySpriteImage.png");

	newSprite->doStuffForMySprite();

	return newSprite;
	
}

void MySprite::doStuffForMySprite()
{
	// .. Doing stuff with my Sprite
}

The error appears on the line
MySprite* newSprite = MySprite::create(“mySpriteImage.png”);

a value of type "cocos2d::Sprite * cannot be used to initialize an entity of type "MySprite *"

What @R101 said, makes sense. You can’t make a derived class pointer to point to a parent class type, but reverse of it is perfectly legal.
So, just implement

MySprite::create()

to get rid off error you mentioned.

1 Like

You are passing a file name as a parameter but you don’t have it declared in your header that way

1 Like

O.K, that does make sense.

Thank you.

A MySprite is a Sprite, because it derives from Sprite, but a Sprite is not a MySprite since it does not derive from MySprite.

You can only assign an object, B, to another object, A, if and only if B derives from A.

I’m sure there is plenty of info online regarding class inheritance, and it doesn’t matter which OO language you’re using, the concepts are the same.

2 Likes

There can be many implementation one you can do as follows:
MySprite.h

#include "cocos2d.h"
  
class MySprite : public cocos2d::Sprite
{
    void doStuffForMySprite();
    
public:

    static MySprite* createMySprite();
};

MySprite.cpp

#include "MySprite.h"
  
MySprite* MySprite::createMySprite()
{

    auto newSprite = new MySprite();
    newSprite->initWithFile("mySpriteImage.png");
    newSprite->doStuffForMySprite();
    newSprite->autorelease();

    return newSprite;
    
}

void MySprite::doStuffForMySprite()
{
    // .. Doing stuff with my Sprite
}
2 Likes