Cocos2D-X學習筆記 1 helloworld

來源:互聯網
上載者:User

盡量避免使用UI,因為要適配IOS、ANDROID、WinPhone等各種平台   =>  新的一個適配各種平台,減小移植難度的遊戲引擎Cocos2D-X


Cocos2D-X     核心主要是使用C++來寫的,外圍有其他的代碼,比如OC、Android、C#


github下載Cocos2D-X 

1、在window作業系統下搭建開發環境,新版本的Cocos2D-X已經沒有.bat檔案,可以從

http://download.csdn.net/detail/joejames/7331707下載一個,配置好visual studio的cocos2dx建立工程外掛程式

2、也可以參見目錄下的README.md配置path,然後用指令碼建立工程


很多遊戲的一些更新包主要使用lua指令碼要實現,這樣替換更加方便,維護起來也方便


AppDelegate.h和.cpp檔案:項目中程式的入口檔案,類似於main函數,在IOS就是程式的入口檔案


bool AppDelegate::applicationDidFinishLaunching()                  程式啟動後將調用這個方法

void AppDelegate::applicationDidEnterBackground()                  遊戲將要進入後台調用這個方法

void AppDelegate::applicationWillEnterForeground()                   遊戲即將進入前台調用這個方法


CC裡一般名字有shared的一般都是使用單例模式,例如下面的導演類


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

    // turn on display 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;


遊戲元素:任何可以呈現出來的元素,例如 情境、層、精靈


CCScene *scene = CCScene::create();   
HelloWorld *layer = HelloWorld::create(); 
scene->addChild(layer);  

addChild方法:把一個遊戲元素放到另一個遊戲元之中,只有把一個遊戲元素放置到其他已經呈現出來的遊戲元素中,它才會呈現出來。

CCDirector運行了scene ,scene 上的layer 就會顯示出來


virtual bool init(); 初始化方法:

        //(1) 對父類進行初始化   

        if ( !CCLayer::init() )   
        {   
            return false;   
        }   

        //(2) 建立菜單並添加到層  
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        this->addChild(pMenu, 1);

        //(3) 建立"Hello World"標籤並添加到層中   
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));
        this->addChild(pLabel, 1);

        //(4) 建立顯示“HelloWorld.png”的精靈並添加到層中   
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        pSprite->setPosition(ccp(size.width/2, size.height/2));
        this->addChild(pSprite, 0);

        bRet = true;


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.