cocos2d-x 初探helloWorld

cocos2d-x的main函数代码很少,把一些复杂的接口封装到AppDelegate类里了,“AppDelegate”从词意可以得出是app的代理类,而一些最早的场景都会在AppDelegate类里实现。我们先看一下main.cpp

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

#ifdef USE_WIN32_CONSOLE
    AllocConsole();
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
#endif

    // create the application instance
    AppDelegate app;                                    //实例化一个cocos2d_x程序
    CCEGLView* eglView = CCEGLView::sharedOpenGLView(); //创建一个视口
    eglView->setFrameSize(600, 320);                    //设置视口的大小

    int ret = CCApplication::sharedApplication()->run(); //运行cocos2d_x的实例

#ifdef USE_WIN32_CONSOLE
    FreeConsole();
#endif

    return ret;
}

AppDelegate类的实现如下:

class  AppDelegate : private cocos2d::CCApplication
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    /**
    @brief    Implement CCDirector and CCScene init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    */
    //程序启动时自动进入的函数在这里我们一般初始化场景
    virtual bool applicationDidFinishLaunching();

    /**
    @brief  The function be called when the application enter background
    @param  the pointer of the application
    */
    //程序进入后台时执行的操作
    virtual void applicationDidEnterBackground();

    /**
    @brief  The function be called when the application enter foreground
    @param  the pointer of the application
    */
    //程序从后台到前台执行调用的函数
    virtual void applicationWillEnterForeground();
};

在主函数里 实例化 AppDelegate 时会自动调用   

virtual bool applicationDidFinishLaunching();
(为什么会到用它?以后会深入研究)
函数的实现:

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // turn on display FPS
    //打开帧的显示(右下角的数字)
    pDirector->setDisplayStats(false);

    // set FPS. the default value is 1.0/60 if you don't call this
    //设置FPS
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    //创建helloWorld场景
    CCScene *pScene = HelloWorld::scene();

    // run
    //执行场景
    pDirector->runWithScene(pScene);
    return true;
}

helloworld的场景类如下:

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    //但调用create成员函数式会自动执行init函数
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    //返回helloworld场景
    static cocos2d::CCScene* scene();
    
    // a selector callback
    //关闭场景函数
    void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};
CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        //创建一个场景
        scene = CCScene::create();
        //判断场景是否创建成功
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        //创建一个helloworld布景,这时会会调用init()
        HelloWorld *layer = HelloWorld::create();
        //判断helloworld布景是否成功
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        //添加helloworld布景到场景
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CCLabelTTF* pLabel2 = CCLabelTTF::create("My name is CharlesXue","Arial",20);
        CC_BREAK_IF(! pLabel2);

        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));
        pLabel2->setPosition(ccp(size.width / 2,size.height - 70));


        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);
        this->addChild(pLabel2,1);
        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}
原文地址:https://www.cnblogs.com/onlycxue/p/3258519.html