Please see how to implement a class HelloWorld (HelloWorldScene.h and HelloWorldScene.cpp).
Pay special attention to virtual bool init(); and CREATE_FUNC(HelloWorld);.
I used HelloWorldScene.h/cpp like a template and it seems to work. I’m afraid if I don’t understand how exactly cocos2d-x works (or at least, this topic), I’ll get into serious troubles in a near future.
Is there any guide/tutorial talking about this topic in particular? I would like to know why, for example, the class scene has to call itself in one of its methods.
I miss some kind of readings of this type (maybe I don’t know how to search correctly )
I do not remember where I read about it and I was not able to Google it is fast.
I will try to explain the basic idea. Very briefly. No, very very briefly.
1 . In the cocos you have the opportunity to not to care about memory allocation for objects in the scene and when this memory should be freed.
To use this opportunity you should use someCocosObj = SomeCocosClass::create() and someParent->addChild(comeCocosObj) and someParent->removeChild(comeCocosObj).
And do not use someCocosObj = new SomeCocosClass().
2 . If you want to use your own class inherited from cocos class, then you need to make sure that your class has the correct create() function.
Fortunately for you, there is a macro that will create the right create() function for you. It is CREATE_FUNC(YourClassName).
3 . But it uses the function init() inside. So you need to override the init() function for your class. Just do in init() all that you are trying to do in the constructor. And do not declare and do not define a constructor at all. It is enough to have a default constructor.
I mentioned the HelloWorld because it uses this mechanism.
I hope this helps. If you have more questions please ask.
p.s. in your code you try to use layer, but you not create it.
the standard way of doing things in cocos2d is that:
there has to be an init() method for Layer initializations.
there has to be a static createScene() method in your class which will make a scene then make a layer and add it to the scene as a child(remember that layers too contain children like sprites, labels, etc) then at the end of this method we return a reference to that scene that contains our layer so we can give it later to the director which handles showing our scene and cleaning its memory when it terminates.
and you’re clearly missing! (createScene method).
here is an answer that is not mine but it’s useful to read
Cocos2d-x has its memory allocation model as a two-step process, like objective-c. Each object has memory allocated (usually using a "create" method) and then has its state initialized (usually using a method called "init"). So in the create/init methods, you allocate the memory and do any object initialization necessary for it to run.
When the object starts to be put onto the display, or when it is added to another container, its "onEnter" method is called. This gets called when a CCScene/CCLayer (either of which may be containing your CCNode derived object) is displayed by the framework itself.
There are at least 2 patterns for memory allocation and object creation, I tend to follow the pattern of having a class contain a static factory method and a private constructor so that it is unambiguous that you must create the objects through the factory and cannot create one yourself.
// here is how a basic class template should look like
class TestScene : public cocos2d::Layer
{
public:
//here is the createScene method that we are going to use later with the director
static cocos2d::Scene* createScene();
bool init() override;
//this is equivalent to TestScene::create() which is automatically defined for you!
/* why it's automatically defined for us because how cocos standard works that we always need a createScene() method which needs in turn a Layer create method
thats why cocos2d guys gave us a quick way of defining layers create method */
CREATE_FUNC(TestScene);
private:
// 99% of the time your gonna need these properties to position your nodes
cocos2d::Size visibleSize;
cocos2d::Size winSize;
cocos2d::Vec2 center;
};
// here is how the createScene() looks like
Scene* TestScene::createScene()
{
auto scene = Scene::create();
auto layer = TestScene::create();//inside this method init() gets called
scene->addChild(layer);
return scene;//return reference to the scene object to be used later with the director
}
bool TestScene::init()
{
if (!Layer::init()) return false;
this->winSize = Director::getInstance()->getWinSize();
this->visibleSize = Director::getInstance()->getVisibleSize();
this->center = winSize / 2;
///////////////////////////
// you initialization code here!
return true;
}
here are some good book titles that i advise you to read:
Cocos2d-x-c++ Game Development Essentials by sonar sys (noobs level)
Building Android Games with Cocos2d-x-c++(beginner level)
Cocos2d-x by Example, 2nd Edition(advanced level)
Cocos2d-x Cookbook(advanced level)
here is another written tutorial good and well organized
and here is a good pro introductory video on cocos2d-x from microsoft academy: