// create a scene. it's an autorelease object Scene *scene = HelloWorld :: createScene (); // run director- runWithScene (scene); 那么接下來,我們看看這場戲到底內部是執行流程的
// create a scene. it's an autorelease object
Scene *scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
那么接下來,我們看看這場戲到底內部是執行流程的啊。
OK,首先看看HelloWorldScene.h 到底有什么東西。
靜態創建函數
static cocos2d::Scene* createScene();
初始化
virtualbool init();
菜單的一個回調函數
void menuCloseCallback(cocos2d::Ref* pSender);
這個。。看宏定義上面的注釋說是創建一個特定的類
CREATE_FUNC(HelloWorld);
/** * 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 __TYPE__(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = NULL; \ return NULL; \ } \ }
CREATE_FUNC(HelloWorld);
就是相當于
在 HelloWorldScene.h 的定義
static HelloWorld* create();
在 HelloWorldScene.m 的實現
HelloWorld* HelloWorld::create() { //創建一個 HelloWorld 對象 HelloWorld* helloWorld = new HellWorld(); //判斷 HelloWorld 對象是否創建以及初始化成功 if (helloWorld && helloWorld->init()) { //創建成功,初始化成功后,讓其自動釋放內存 helloWorld->autorelease(); //返回 HelloWorld 實例 return helloWorld; } else { //如果創建失敗,將安全刪除 HelloWorld 對象 delete helloWorld; helloWorld = NULL; return NULL; } }
接下來我們看看其他的吧
HelloWorldScene.cpp 的里面的函數的執行順序是
先
Scene* HelloWorld::createScene();
再
bool HelloWorld::init();
Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auro layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; }
Scene* HelloWorld::createScene() { // 'scene' is an autorelease object Scene* scene = Scene::create(); // 'layer' is an autorelease object Layer* layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->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 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); return true; }
從表面上看,這段代碼都在講初始化的那些事。
細心觀察,這個和Objective-C 的 init 方法多類似啊,只是不是返回對象。
我們精簡一下這段代碼的框架
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } //初始化的內容 return true; }
接下來看看里面進行的初始化的內容吧
向導演問了相關舞臺的數據
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->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 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1);
// 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 MenuItemImage* closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object Menu* menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1);
MenuItemImage 類 創建一個對象,放入兩張圖片,和一個回調函數。
第一張圖片是正常狀態的,第二張是選擇狀態時的,回調函數,this 應該是目標和iOS 創建按鈕很相似,而區別是沒有觸發事件的手勢設置。
接下來就是設置 MenuItemImage 類 實例的位置
通過 MenuItemImage 類 實例 創建一個 Menu 類的實例。
設置坐標
最后,將這個Menu類的實例加入當前 Layer中
接下來就是創建一個Label 類了。
根據官方發布文檔所描述。3.0將采用一個Label 類 來創建不同類型的Label,而且優化了很多性能,這些也是后話了。
// 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1);
創建一個LabelTTF類的實例,參數1是內容,參數2是字體,參數3是字體大小
然后就是設置 這個實例的位置
然后加入層
// add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0);
首先用精靈創建一個實例,參數是一張圖片。
然后設置精靈的位置。
最后把精靈加入層里,
最后我們看看回調函數吧,當點擊按鈕時,就會觸發這個回調函數,因為已經關聯上了。
void HelloWorld::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
好了,就這樣就結束了。接下來就是詳情了。
表面上看,Cocos2d-X 真的不難~~~
呵呵
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com