Learning Cocos2d-x for WP8(2)——深入刨析Hello World

cocos2d-x框架

在兄弟篇Learning Cocos2d-x for XNA1——小窥cocos2d-x框架中已有详细介绍cocos2d-x框架下的基本元素。可自行参考学习,概念的东西基本一样。

HelloWorld

HelloWorld程序虽然简单,但能测试程序是否能正确的运行,同时很能体现一个框架的整体结构。

cocos2d-x中HelloWorld显示主要通过AppDelegate和HelloWorldScene。

在显示HelloWorld程序时,得将图片资源放进Assets文件夹中

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第1张图片

AppDelegate

C++程序中最主要的是头文件(.h)和源文件(.cpp)。

AppDelegate.h

AppDelegate.h头文件中,定义类(Class)AppDelegate继承CCApplication。在AppDelegate中声明相关方法。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第2张图片

AppDelegate.cpp

AppDelegate.cpp源文件中包含AppDelegate.h头文件,在其中实现头文件中声明的方法。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第3张图片

其中在方法applicationDidFinishLaunching中,我们找到了熟悉的CCDirector(导演),当中pScene为起始页面。很显然HelloWorld类,需要在AppDelegate.cpp中引用Classes文件夹中的HelloWorldScene.h头文件(fei话,呵呵)。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第4张图片

HelloWorldScene

scene()方法

主要负责将Layer层通过addChild到Scene层

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第5张图片

init()方法

主要将Layer层以下层内容添加到Layer层。

HelloWorld::init()
 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完整代码

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完整代码

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 }

Learning Cocos2d-x for WP8(2)——深入刨析Hello World_第6张图片

版本cocos2dx-0.13.0-wp8-0.8似乎不怎么给力,Lumia820真机上测试不通过,模拟器没任何问题。不过快有新版本出来了吧,现在凑合学习学习。

 

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

你可能感兴趣的:(world,hello)