cocos2d-x Scene的启动加载

在cocos2d-x的自带例子中,我们通过 scene()这个函数来返回一个CCScene指针,但在这个函数中又会调用别的函数,我加了几个打印输出来大概的跟踪一一流程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CCScene* HelloWorld::scene()
{
CCLog( "HelloWorld::scene" );
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
CCLog( "HelloWorld::create before" );
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CCLog( "HelloWorld::create after" );
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
while  (0);
// return the scene
CCLog( "scence end" );
return  scene;
}


CC_BREAK_IF(! scene); 的展开为:

1
#define CC_BREAK_IF(cond)            if(cond) break

意思是如果没有生成,便跳出结束程序的执行。


在函数init()中我也加了一个打印输出,

1
2
3
4
5
6
7
8
9
bool  HelloWorld::init()
{
CCLog( "HelloWorld::init" );
bool  bRet =  false ;
do
{
     .......
}
}

最后的输出为:

HelloWorld::scene
HelloWorld::create before
?HelloWorld::init
HelloWorld::create after
scence end

可以在执行HelloWorld::create();时,调用了HelloWorld::init().而在init这个方法中又有大量的加载及初始化操作。

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


下面的代码为生成一个关闭铵钮,最后一个参数为回调函数,就是我们点击这个按钮时需要执行的操作,我们这里需要重写HelloWorld::menuCloseCallback这个方法,因为 不同程序的关闭会执行不同的操作,有的会保存上下文,例如玩家的一些状态,而不是只是单纯的退出程序完事。

1
2
3
4
5
​CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
     "CloseNormal.png" ,
     "CloseSelected.png" ,
     this ,
     menu_selector(HelloWorld::menuCloseCallback));


下面的一行代码为设置关闭钮的位置,这里设置为右下角,我们首先获取宽度然后减去20,高度我们设置为20,记住这里的坐标轴是左下角开始的。

1
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));


接着我们把CCMenuItemImage加入到CCMenu,CCMenuItemImage只是一个图标,相当于一个数据,而CCMenu相当于一个大的框架。

1
2
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);

其中的CCPointZero为点0,0,这里我有一个疑问,为什么这里CCMenu会放到右下角,按照道理来讲是左下角?


接一来我们添加HelloWorld这个字符,

1
2
3
4
5
6
7
8
// Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::create( "Hello World" "Arial" , 24);
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
// Add the label to HelloWorld layer as a child layer.
this ->addChild(pLabel, 1);


接下来添加一个精灵:

1
2
3
4
5
6
7
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create( "HelloWorld.png" );
CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this ->addChild(pSprite, 0);

类似于上面添加CCMenu,精灵相当于一些会移动的东西,我们来能过代码控制它的移动。 


当我们点击右下角的按钮时便会触发前面我们提到的回调函数,这个结束事件的代码为:

1
2
3
4
5
void  HelloWorld::menuCloseCallback(CCObject* pSender)
{
     // "close" menu item clicked
     CCDirector::sharedDirector()->end();
}



既然提到精灵,接下来学习一下如何操作精灵。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值