0
\$\begingroup\$

I am confused about something. I have the following example:

mainMenu.h

class mainMenu : public Node {
public:
    static Scene* createScene();
    virtual bool init();
    void GoToGameScene(Ref *pSender);
    CREATE_FUNC(mainMenu);


private:
    mainMenu() {};
    ~mainMenu() {};


};

mainMenu.cpp

Scene* mainScene::createScene(){
    auto scene = Scene::create();
    auto layer = mainScene::create();
    scene->addChild(layer);

    return scene;
}

Focus on this part.

auto scene = Scene::create();
auto layer = mainScene::create();

Why do we create layer from mainScene not from Layer

For example: Why we do this:

auto layer = mainScene::create();

If we can do it, like creating a scene

auto layer = Layer::create();
\$\endgroup\$
1
\$\begingroup\$

First of all, it's very uncommon to start classnames with a lowercase letter. It makes the code harder to understand.

Second, I'm not sure if I got your question right, but it seems like you're asking: Why call a create() method on the mainScene class instead of just using a simple Layer::create()?

If that's the case then the answer is quite simply: Because in this way we can customize how the mainScene should be initialized. Perhaps it needs several layers, like a HUD layer, a game layer, a controls layer, etc. By calling its create() method we make sure the constructor of mainScene is called, but also it's created as a Ref object, meaning that it'll be reference counted and therefore the memory management will be done automagically (for the most part).

If that wasn't the question feel free to explain it to me a bit further.

\$\endgroup\$
  • \$\begingroup\$ C++ Has no common conventions. He can write however he wants. This is also the main reason why C++ is such a terrible language. \$\endgroup\$ – Krythic Mar 3 '17 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.