How do you draw a custom class?

I have the below class definition:

Creature.h


#include "cocos2d.h"

class Creature : public cocos2d::Node
{
public:
    Creature();
    ~Creature();
    cocos2d::Vec2 get_position();
    int get_x();
    int get_y();
    void set_y_position(const int);
    void set_x_position(const int);
    void set_position(const cocos2d::Vec2);

    void draw();
private:
    cocos2d::Sprite* m_creature_sprite;
    cocos2d::Vec2 m_position;
};

I am fairly new and do not know the best practices with the engine. What do I need to do in the overridden cocos2d::Node draw method above to ensure that my class gets drawn? Is doing the above even recommended, or should I use a factory that returns a cocos2d::Sprite*?

if you want to override draw()

then in your cpp

void Creature::draw()
{
// whatever you need to do here.
}

I think your title may be a bit confusing.

If you actually want to draw m_creature_sprite, you do not have to do anything special. Just be sure the sprite is on the node’s hierarchy.
For example:

in Creature constructor:
m_creature_sprite = Sprite::create(“my_sprite.png”);
addChild(m_creature_sprite);

thats it!

if you really want to make a custom draw method for your class, then follow slackmoehrle’s advice

1 Like

also, let me point out that you don’t actually need these at all, probably.

you can always do:

m_creature_sprite->getPosition();
m_creature_sprite->getPositionX();
m_creature_sprite->getPositionY();

same with all the set...(), etc

1 Like

Slackmoehrle for overriding the draw method I knew to do that, I just didn’t know if there was something special I needed to do. I didn’t read all the class documentation and didn’t know that I inherited the get and set position functions, thanks for that heads up.

Dredok I apologize for the confusing title. Adding the sprite as a child in the constructor worked, thank you. I didn’t think it could be something that easy which is why I was wanting to override the draw method.

Thank you both for your help.