cocos2d-x框架
在兄弟篇Learning Cocos2d-x for XNA(1)——小窺cocos2d-x框架中已有詳細介紹cocos2d-x框架下的基本元素。可自行參考學習,概念的東西基本一樣。
HelloWorld
HelloWorld程序雖然簡單,但能測試程序是否能正確的運行,同時很能體現一個框架的整體結構。
cocos2d-x中HelloWorld顯示主要通過AppDelegate和HelloWorldScene。
在顯示HelloWorld程序時,得將圖片資源放進Assets文件夾中
AppDelegate
C++程序中最主要的是頭文件(.h)和源文件(.cpp)。
AppDelegate.h
AppDelegate.h頭文件中,定義類(Class)AppDelegate繼承CCApplication。在AppDelegate中聲明相關方法。
AppDelegate.cpp
AppDelegate.cpp源文件中包含AppDelegate.h頭文件,在其中實現頭文件中聲明的方法。
其中在方法applicationDidFinishLaunching中,我們找到了熟悉的CCDirector(導演),當中pScene為起始頁面。很顯然HelloWorld類,需要在AppDelegate.cpp中引用Classes文件夾中的HelloWorldScene.h頭文件(fei話,呵呵)。
HelloWorldScene
scene()方法
主要負責將Layer層通過addChild到Scene層
init()方法
主要將Layer層以下層內容添加到Layer層。

1 bool HelloWorld::init() 2 { 3 bool bRet = false; 4 5 do 6 { 7 if ( !CCLayer::init() ) 8 { 9 break; 10 } 11 this->setIsTouchEnabled(true); 12 13 CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); 14 15 // 1. Add a menu item with "X" image, which is clicked to quit the program. 16 17 // Create a "close" menu item with close icon, it's an auto release object. 18 CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( 19 "CloseNormal.png", 20 "CloseSelected.png", 21 this, 22 menu_selector(HelloWorld::menuCloseCallback)); 23 CC_BREAK_IF(! pCloseItem); 24 25 // Place the menu item bottom-right conner. 26 pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); 27 28 // Create a menu with the "close" menu item, it's an auto release object. 29 CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); 30 pMenu->setPosition(CCPointZero); 31 CC_BREAK_IF(! pMenu); 32 33 // Add the menu to HelloWorld layer as a child layer. 34 this->addChild(pMenu, 1); 35 36 // 2. Add a label shows "Hello World". 37 38 // Create a label and initialize with string "Hello World". 39 CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24); 40 CC_BREAK_IF(! pLabel); 41 42 // Get window size and place the label upper. 43 CCSize size = CCDirector::sharedDirector()->getWinSize(); 44 pLabel->setPosition(ccp(size.width / 2, size.height - 50)); 45 46 // Add the label to HelloWorld layer as a child layer. 47 this->addChild(pLabel, 1); 48 49 // 3. Add add a splash screen, show the cocos2d splash image. 50 CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); 51 CC_BREAK_IF(! pSprite); 52 53 // Place the sprite on the center of the screen 54 pSprite->setPosition(ccp(size.width/2, size.height/2)); 55 56 // Add the sprite to HelloWorld layer as a child layer. 57 this->addChild(pSprite, 0); 58 59 bRet = true; 60 } while (0); 61 62 return bRet; 63 }
HelloWorldScene.h完整代碼

1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include "SimpleAudioEngine.h" 6 7 class HelloWorld : public cocos2d::CCLayer 8 { 9 public: 10 HelloWorld(); 11 ~HelloWorld(); 12 13 // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 14 virtual bool init(); 15 16 // there's no 'id' in cpp, so we recommand to return the exactly class pointer 17 static cocos2d::CCScene* scene(); 18 19 // a selector callback 20 void menuCloseCallback(CCObject* pSender); 21 22 // implement the "static node()" method manually 23 LAYER_NODE_FUNC(HelloWorld); 24 }; 25 26 #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp完整代碼

1 #include "pch.h" 2 #include "HelloWorldScene.h" 3 4 using namespace cocos2d; 5 6 HelloWorld::~HelloWorld() 7 { 8 // cpp don't need to call super dealloc 9 // virtual destructor will do this 10 } 11 12 HelloWorld::HelloWorld() 13 { 14 } 15 16 CCScene* HelloWorld::scene() 17 { 18 CCScene * scene = NULL; 19 do 20 { 21 // 'scene' is an autorelease object 22 scene = CCScene::node(); 23 CC_BREAK_IF(! scene); 24 25 // 'layer' is an autorelease object 26 HelloWorld *layer = HelloWorld::node(); 27 CC_BREAK_IF(! layer); 28 29 // add layer as a child to scene 30 scene->addChild(layer); 31 } while (0); 32 33 // return the scene 34 return scene; 35 } 36 37 // on "init" you need to initialize your instance 38 bool HelloWorld::init() 39 { 40 bool bRet = false; 41 42 do 43 { 44 if ( !CCLayer::init() ) 45 { 46 break; 47 } 48 this->setIsTouchEnabled(true); 49 50 CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); 51 52 // 1. Add a menu item with "X" image, which is clicked to quit the program. 53 54 // Create a "close" menu item with close icon, it's an auto release object. 55 CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( 56 "CloseNormal.png", 57 "CloseSelected.png", 58 this, 59 menu_selector(HelloWorld::menuCloseCallback)); 60 CC_BREAK_IF(! pCloseItem); 61 62 // Place the menu item bottom-right conner. 63 pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); 64 65 // Create a menu with the "close" menu item, it's an auto release object. 66 CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); 67 pMenu->setPosition(CCPointZero); 68 CC_BREAK_IF(! pMenu); 69 70 // Add the menu to HelloWorld layer as a child layer. 71 this->addChild(pMenu, 1); 72 73 // 2. Add a label shows "Hello World". 74 75 // Create a label and initialize with string "Hello World". 76 CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24); 77 CC_BREAK_IF(! pLabel); 78 79 // Get window size and place the label upper. 80 CCSize size = CCDirector::sharedDirector()->getWinSize(); 81 pLabel->setPosition(ccp(size.width / 2, size.height - 50)); 82 83 // Add the label to HelloWorld layer as a child layer. 84 this->addChild(pLabel, 1); 85 86 // 3. Add add a splash screen, show the cocos2d splash image. 87 CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); 88 CC_BREAK_IF(! pSprite); 89 90 // Place the sprite on the center of the screen 91 pSprite->setPosition(ccp(size.width/2, size.height/2)); 92 93 // Add the sprite to HelloWorld layer as a child layer. 94 this->addChild(pSprite, 0); 95 96 bRet = true; 97 } while (0); 98 99 return bRet; 100 } 101 102 void HelloWorld::menuCloseCallback(CCObject* pSender) 103 { 104 // "close" menu item clicked 105 CCDirector::sharedDirector()->end(); 106 }
版本cocos2dx-0.13.0-wp8-0.8似乎不怎么給力,Lumia820真機上測試不通過,模擬器沒任何問題。不過快有新版本出來了吧,現在湊合學習學習。
著作權聲明:本文由http://www.cnblogs.com/suguoqiang 原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝!