cocos2d-x进度条读取

转自:http://blog.csdn.net/xiaoxiangp/article/details/7706764

昨天下载了最新的cocos2d-x   是2.0.1版本的...所以本文章是参考2.0版本的例子写的...





可以发现,2.0版本后不单单只有一个帧率了..还多了两个参数.第一个参数可以理解为场景中的对象个数吧.第二个还不清楚...


1. 创建好一个工程后,先修改对于的.h文件

[cpp]  view plain copy
  1. class HelloWorld : public cocos2d::CCLayer  
  2. {  
  3. public:  
  4.     virtual bool init();    
  5.     static cocos2d::CCScene* scene();  
  6.     HelloWorld();  
  7.   
  8.     void addSprite();//用于加载完成后添加精灵  
  9.     void loadingCallBack(cocos2d::CCObject *obj);//读进度回调函数  
  10.   
  11.     LAYER_CREATE_FUNC(HelloWorld);  
  12.   
  13. private:  
  14.     cocos2d::CCLabelTTF *m_pLableLoading;  
  15.     cocos2d::CCLabelTTF *m_pLabelPercent;  
  16.     int m_nNumberOfSprites;  
  17.     int m_nNumberOfLoadedSprites;  
  18. };  

2. 然后在对应的cpp文件中完成各种函数


[cpp]  view plain copy
  1. //原例子中的代码都是在构造函数中写的.在这里构造函数只用于初始化了两个成员变量  
  2. HelloWorld::HelloWorld():m_nNumberOfSprites(20),m_nNumberOfLoadedSprites(0)  
  3. {  
  4.   
  5. }  


[cpp]  view plain copy
  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         CC_BREAK_IF(! CCLayer::init());  
  7.         CCSize size = CCDirector::sharedDirector()->getWinSize();  
  8.   
  9.         //创建读条时的标签  
  10.         m_pLableLoading = CCLabelTTF::create("Loading...","Arial",20);  
  11.         m_pLabelPercent = CCLabelTTF::create("%0","Arial",30);  
  12.           
  13.         m_pLabelPercent->setPosition(CCPointMake(size.width/2, size.height/2 +50));  
  14.         m_pLableLoading->setPosition(CCPointMake(size.width/2, size.height/2 -10));  
  15.   
  16.         this->addChild(m_pLableLoading);  
  17.         this->addChild(m_pLabelPercent);  
  18.   
  19.         //加载图片到Cache...每加载一次就调用我们自己写的回调函数  
  20.         CCTextureCache::sharedTextureCache()->addImageAsync("img/bg.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  21.         CCTextureCache::sharedTextureCache()->addImageAsync("img/01.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  22.         CCTextureCache::sharedTextureCache()->addImageAsync("img/02.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  23.         CCTextureCache::sharedTextureCache()->addImageAsync("img/03.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  24.         CCTextureCache::sharedTextureCache()->addImageAsync("img/04.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  25.         CCTextureCache::sharedTextureCache()->addImageAsync("img/05.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  26.         CCTextureCache::sharedTextureCache()->addImageAsync("img/06.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  27.         CCTextureCache::sharedTextureCache()->addImageAsync("img/07.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  28.         CCTextureCache::sharedTextureCache()->addImageAsync("img/08.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  29.         CCTextureCache::sharedTextureCache()->addImageAsync("img/09.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  30.         CCTextureCache::sharedTextureCache()->addImageAsync("img/10.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  31.         CCTextureCache::sharedTextureCache()->addImageAsync("img/11.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  32.         CCTextureCache::sharedTextureCache()->addImageAsync("img/12.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  33.         CCTextureCache::sharedTextureCache()->addImageAsync("img/13.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  34.         CCTextureCache::sharedTextureCache()->addImageAsync("img/14.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  35.         CCTextureCache::sharedTextureCache()->addImageAsync("img/15.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  36.         CCTextureCache::sharedTextureCache()->addImageAsync("img/16.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  37.         CCTextureCache::sharedTextureCache()->addImageAsync("img/17.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  38.         CCTextureCache::sharedTextureCache()->addImageAsync("img/18.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  39.         CCTextureCache::sharedTextureCache()->addImageAsync("img/19.png"this, callfuncO_selector(HelloWorld::loadingCallBack));  
  40.   
  41.         bRet = true;  
  42.     } while (0);  
  43.   
  44.     return bRet;  
  45. }  

3. 代码很多都是重复的..只是加载的图片不一样,大家注意加载的图片路径就可以了..接下来实现回调函数


[cpp]  view plain copy
  1. //回调函数  
  2. void HelloWorld::loadingCallBack(CCObject *obj)  
  3. {  
  4.     ++m_nNumberOfLoadedSprites;//每调用一次就+1  
  5.     char tmp[10];  
  6.   
  7.     //这里格式化,因为我们只加载了20张图片,所以除以总数后就是0.05,所以每加载一次就涨5%  
  8.     sprintf(tmp,"%%%d",(int)(((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100));  
  9.     m_pLabelPercent->setString(tmp);//重设标签的内容  
  10.   
  11.     //判断,如果全部加载完成后,就执行  
  12.     if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)  
  13.     {  
  14.         //移除那两个标签文字..  
  15.         this->removeChild(m_pLableLoading, true);  
  16.         this->removeChild(m_pLabelPercent, true);  
  17.         //调用自己写的函数..从新绘制场景内容  
  18.         addSprite();  
  19.     }  
  20.       
  21. }  

4. 实现addSprite()函数,这个函数从新初始化了场景..然后显示


[cpp]  view plain copy
  1. void HelloWorld::addSprite()  
  2. {  
  3.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  4.   
  5.     //创建背景图片  
  6.     CCSprite *bg = CCSprite::create("img/bg.png");  
  7.     bg->setPosition(CCPointMake(size.width/2, size.height/2));  
  8.   
  9.     //创建其他小图片  
  10.     CCSprite *s1 = CCSprite::create("img/01.png");  
  11.     CCSprite *s2 = CCSprite::create("img/02.png");  
  12.     CCSprite *s3 = CCSprite::create("img/03.png");  
  13.     CCSprite *s4 = CCSprite::create("img/04.png");  
  14.     CCSprite *s5 = CCSprite::create("img/05.png");  
  15.     CCSprite *s6 = CCSprite::create("img/06.png");  
  16.     CCSprite *s7 = CCSprite::create("img/07.png");  
  17.     CCSprite *s8 = CCSprite::create("img/08.png");  
  18.     CCSprite *s9 = CCSprite::create("img/09.png");  
  19.     CCSprite *s10 = CCSprite::create("img/10.png");  
  20.     CCSprite *s11 = CCSprite::create("img/11.png");  
  21.     CCSprite *s12 = CCSprite::create("img/12.png");  
  22.     CCSprite *s13 = CCSprite::create("img/13.png");  
  23.     CCSprite *s14 = CCSprite::create("img/14.png");  
  24.     CCSprite *s15 = CCSprite::create("img/15.png");  
  25.     CCSprite *s16 = CCSprite::create("img/16.png");  
  26.     CCSprite *s17 = CCSprite::create("img/17.png");  
  27.     CCSprite *s18 = CCSprite::create("img/18.png");  
  28.     CCSprite *s19 = CCSprite::create("img/19.png");  
  29.   
  30.     //设置位置  
  31.     s1->setPosition(CCPointMake(70, size.height/2));  
  32.     s2->setPosition(CCPointMake(90, size.height/2));  
  33.     s3->setPosition(CCPointMake(110, size.height/2));  
  34.     s4->setPosition(CCPointMake(130, size.height/2));  
  35.     s5->setPosition(CCPointMake(150, size.height/2));  
  36.     s6->setPosition(CCPointMake(170, size.height/2));  
  37.     s7->setPosition(CCPointMake(190, size.height/2));  
  38.     s8->setPosition(CCPointMake(210, size.height/2));  
  39.     s9->setPosition(CCPointMake(230, size.height/2));  
  40.     s10->setPosition(CCPointMake(250, size.height/2));  
  41.     s11->setPosition(CCPointMake(270, size.height/2));  
  42.     s12->setPosition(CCPointMake(290, size.height/2));  
  43.     s13->setPosition(CCPointMake(310, size.height/2));  
  44.     s14->setPosition(CCPointMake(330, size.height/2));  
  45.     s15->setPosition(CCPointMake(350, size.height/2));  
  46.     s16->setPosition(CCPointMake(370, size.height/2));  
  47.     s17->setPosition(CCPointMake(390, size.height/2));  
  48.     s18->setPosition(CCPointMake(410, size.height/2));  
  49.     s19->setPosition(CCPointMake(430, size.height/2));  
  50.   
  51.     //添加进入场景  
  52.     this->addChild(bg);  
  53.   
  54.     this->addChild(s1);  
  55.     this->addChild(s2);  
  56.     this->addChild(s3);  
  57.     this->addChild(s4);  
  58.     this->addChild(s5);  
  59.     this->addChild(s6);  
  60.     this->addChild(s7);  
  61.     this->addChild(s8);  
  62.     this->addChild(s9);  
  63.     this->addChild(s10);  
  64.     this->addChild(s11);  
  65.     this->addChild(s12);  
  66.     this->addChild(s13);  
  67.     this->addChild(s14);  
  68.     this->addChild(s15);  
  69.     this->addChild(s16);  
  70.     this->addChild(s17);  
  71.     this->addChild(s18);  
  72.     this->addChild(s19);  
  73. }  

这样,运行的时候就会出现读条界面了,但是闪的很快,比较电脑的处理能力还是很快的嘛..可以在回调函数里面设置一个断点,就会发现是5%的增长..


就我的理解来说,这一个例子的思路,首先是把图片加载进入内存.然后出发一次回调函数,更新读条进度..直到100%就调用另外函数...


图片的话,随便弄个几张就好了.显示效果的话,注意设置位置就行,也可以用我的图片啦:

http://115.com/file/anpzdgqa#img.rar


by...XX


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值