天天看點

【Cocos2d-x】Cocos2d-x參考案例源碼解析之三:HelloWorld

允許我講些與源碼無關的事情 - -!稍後K我

由于cocos2dX3.0快出來了,官網上說放棄objective c風格,本屌學的是C++,是以真是太高興了,是以放慢的源碼解析 - -(其實給自己找理由,最多變下類名- -),決定直接講掉HelloWorld進行實際開發。

好了,其實這幾天也沒閑,遊戲開發如果不想window MFC有控件的話,單純代碼cocos2dX代碼寫控件的話那是很吓人的,你會寫上幾百行隻為實作一個按鈕效果。好了,我是要引出CocoStuido工具,這個工具很強大,觸控集團開發的,有場景編輯器,UI編輯器,動作動畫編輯了,這樣我們程式猿就不必糾結界面的設計布局,交給工具做吧,我們隻要考慮邏輯了,是不是大大簡化了程式員的工作呢。

關于CocoStudio下載下傳:http://www.cocostudio.org/

工具使用介紹:

UI編輯器使用者指引:  http://www.cocoachina.com/bbs/read.php?tid=161578&keyword=CocoStudio%BD%CC%B3%CC

Action編輯器使用者指引: http://www.cocoachina.com/bbs/read.php?tid=161600&keyword=CocoStudio%BD%CC%B3%CC

Scene編輯器使用者指引:  http://www.cocoachina.com/bbs/read.php?tid=161606&keyword=CocoStudio%BD%CC%B3%CC

-------------------------------------------------------好了,我們來講HelloWorld-------------------------------------------------------------------

當然,還是從Main入口開始看

eglView->setFrameSize(2048, 1536);
    // The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
    // So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
    eglView->setFrameZoomFactor(0.4f);
           

設定了螢幕大小為2048,1536;這裡也可以通過Api擷取目前裝置的寬高然後賦予,這裡你看到有個setFrameZoomFactor(0.4f),這個上面解釋是Ipad3的分辨率非常大,調整分辨率使用win32,mac,linux裝置;主要為了實作高清

下面看AppDelegate.cpp

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

    pDirector->setOpenGLView(pEGLView);
	CCSize frameSize = pEGLView->getFrameSize();

    // 設定設計分辨率
	// 下一章我會講為什麼需要這個setDesignResolutionSize函數
	// 這裡調用了AppMacros.h檔案裡的定義,采用的是條件編譯(不知道條件編譯的百度或者留言我再講吧)來  根據不同裝置定義不同的 設定設計分辨率 大小
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
#else
    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);
#endif

    
    vector<string> searchPath;

	// 在這個示範中,我們選擇資源根據幀的身高。
	// 如果資源大小不同設計分辨率大小,你需要用contentScaleFactor設定調整。
	// 我們使用資源的高度比分辨率高度設計的
	// 這可以確定資源的高度能适應的設計分辨率高度。

	// 如果螢幕的高度大于媒體資源高度的大小,選擇大的資源。
	if (frameSize.height > mediumResource.size.height)
	{
		// 資源檔案夾名稱
        searchPath.push_back(largeResource.directory);
		// 縮放設定比列
        pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
	}
    // if the frame's height is larger than the height of small resource size, select medium resource.
    else if (frameSize.height > smallResource.size.height)
    {
		//
        searchPath.push_back(mediumResource.directory);
        
        pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
    }
    // if the frame's height is smaller than the height of medium resource size, select small resource.
	else
    {
        searchPath.push_back(smallResource.directory);

        pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
    }


    // 字母上了解設定搜尋資源路徑
    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
	
    // 開啟 FPS
    pDirector->setDisplayStats(true);

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

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

    // run
    pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();
	// 進入背景調用
    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();
	// 退出背景進入活動狀态前
    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
           

applicationDidEnterBackground()和applicationWillEnterForeground()

你可以分别打斷點F9,然後進入調試狀态F5,啟動界面後縮小對話框 和恢複分别會進入斷點就可以了解了。

CCScene *pScene = HelloWorld::scene();
           

此句調用HelloWorld 的靜态scene函數,HelloWorld派生與cocos2d::CCLayer

下面看HelloWorld.cpp

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
	//調用基類,從上面的官方注釋的英文可以看出基類的Create裡有new操作,并且加入了自動釋放池(關于自動釋放池參考OC)中
    CCScene *scene = CCScene::create();
	// 'layer' is an autorelease object
	// 這裡建立我們的圖層,加入了自動釋放池,進行自動管理不用擔心記憶體洩露
	// 這裡你看HelloWorld的頭檔案并沒有定義靜态函數create()
	// 這是為什麼呢? 但是你看下頭檔案中有這麼一個東西
	// CREATE_FUNC(HelloWorld);
	// 注釋說手動實施靜态節點方法
	// 其實是為了統一定義了一個宏
	// 滑鼠點選在CREATE_FUNC上右鍵,選擇轉到定義或者F12
    // 可以看到和基類的CCScene::create()基本一樣的,裡面調用了init方法
	// 而這個init方法,實作這個圖層的功能,我們可以将相關的實作代碼放在這裡
	// 當然,這是一個虛函數,傳回一個bool
   
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
	// 将圖層加入到場景中
    scene->addChild(layer);

    // return the scene
    return scene;
}
           
【Cocos2d-x】Cocos2d-x參考案例源碼解析之三:HelloWorld

滑鼠點選在create上,右鍵選擇轉到定義,或者點選圖檔右上角的GO,或者按F12檢視create

看bool HelloWorld::init()

{
    //
    // 1. super init first
	// 先初始化超類
    if ( !CCLayer::init() )
    {
        return false;
    }
    
	// 可視區域大小
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	// 表示可視區域的起點坐标
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
	// 建立菜單項圖檔 第一個參數代表一般情況下顯示的圖檔,第二個參數代表按下去的時候顯示的圖檔,
	// 第三個參數代表放在那個曾,
	// 第四個參數是回調函數-靜态函數,用于響應點選這個圖檔時需要響應
	//
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	// 設定菜單項圖檔位置
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
	// 建立菜單
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	// 設定菜機關置
    pMenu->setPosition(CCPointZero);
	// 菜單加入到圖層
    this->addChild(pMenu, 1);

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

    // add a label shows "Hello World"
    // create and initialize a label
    // 建立Lable
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

	// 這裡本人弄了半天 不了解visibleSize.width/2  為什麼除以2,後來才知道- -||
	// 原因是C++的坐标是以左上角為原點,如果設定圖檔的話以圖檔左上角做參考位置來設定
	// 而cocos2dX采用OPenGL左下角為坐标原點,向上增加,向右增加,
    // 預設錨點為0.5,0.5,即質點位置,以錨點為圖檔的參考點
	// 這個設定精靈的圖檔  位置是相對于以精靈的中心點(錨點)位置進行設定的
    // position the sprite on the center of the screen

	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

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

	
    return true;
}
           

剛接觸Cocos2d-x,如有錯誤請大家糾正,共同學習進步 

 程式設計QQ群:160296200

  本篇部落格出Leon,轉載請注明出處: http://blog.csdn.net/leoncoder/article/details/12791143