Wumao's cocos2d-x study notes 02-basic project source code analysis

3232 단어 cocos2d-x
class AppDelegate : private cocos2d::Application private means private inheritance, cocs2d is a namespace. Under private inheritance, the members of the private, protected, and public properties in the Application class will become private in AppDelegate.
The main.cpp entry method has a piece of code: return Application::getInstance()->run(); This code calls the run method of AppDelegate; the run method calls the applicationDidFinishLaunching method of AppDelegate, and the HelloWorld scene class is created in the applicationDidFinishLaunching method auto scene = HelloWorld::createScene();, and run the scene class director->runWithScene(scene);. The createScene method of the HelloWorld scene class calls the static method create method of HelloWorld. Isn't it strange, because this method is not seen in the HelloWorld scene class, in fact, this method is implemented by the macro CREATE_FUNC(HelloWorld);. In the CREATE_FUNC macro definition, the init method of HelloWorld is called, so a scene class must implement at least three methods: createScene, init, and CREATE_FUNC.

Scene* HelloWorld::createScene(){

     auto scene = Scene::create();

     auto layer = HelloWorld::create();// CREATE_FUNC(MyHelloWorldScene);

     scene->addChild(layer);

     return scene;

}



CREATE_FUNC(__TYPE__):

/**

* define a create function for a specific type, such as Layer

* @param \__TYPE__  class type to add create(), such as Layer

*/

#define CREATE_FUNC(__TYPE__) \

static __TYPE__* create() \

{ \

    __TYPE__ *pRet = new(std::nothrow) __TYPE__(); \

    if (pRet && pRet->init()) \

    { \

        pRet->autorelease(); \

        return pRet; \

    } \

    else \

    { \

        delete pRet; \

        pRet = NULL; \

        return NULL; \

    } \

}

The scene itself is meaningless, when we add various layers, sprites, menus to the scene, the scene becomes meaningful.

Scene inherits from Node. The 3.x version of Scene integrates the functionality of the physics engine. So the scene can be seen as a physical world.


But strangely, HelloWorldScene inherits from Layer, not Scene.

좋은 웹페이지 즐겨찾기