cocos2dx 加载 进度条样式首先,我要说的是:这次我要写的主题是进度条。 额,等等,先收起你手里愤怒的西瓜刀。我也才知道TestCpp也有这个例子啊。不过TestCpp里的只有label的变化,而我的多加了个进度条。
恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。



首先看下头文件:HelloWorld.h

  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. class HelloWorld : public cocos2d::Layer  
  7. {  
  8. public:  
  9.     HelloWorld():m_numSp(20),m_loadedSp(0),loadProgress(NULL){};  
  10.   
  11.     static cocos2d::Scene* createScene();  
  12.     virtual bool init();    
  13.   
  14.     void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数  
  15.   
  16.     void gotoNewLayer();//加载完后的跳转函数  
  17.     CREATE_FUNC(HelloWorld);  
  18.   
  19. private:  
  20.     cocos2d::ProgressTimer* loadProgress;//进度条  
  21.   
  22.     cocos2d::LabelTTF* percentLabel;//加载进度label  
  23.     cocos2d::LabelTTF* loadLabel;//显示 loading: 的label  
  24.   
  25.     int m_numSp;//要加载的精灵数目,初始化为 20 张  
  26.     int m_loadedSp;//已加载的精灵数目  
  27. };  
  28.   
  29. #endif // __HELLOWORLD_SCENE_H__  

1、创建
  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2. Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4. loadLabel = LabelTTF::create("Loading:","Arial",20);//创建显示Loading: 的label  
  5. loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));  
  6. this->addChild(loadLabel,1);  
  7.   
  8. percentLabel = LabelTTF::create("0%","Arial",20);//创建显示百分比的label  
  9. percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));  
  10. this->addChild(percentLabel,2);  
  11.   
  12. auto loadBg = Sprite::create("sliderTrack.png");//进程条的底图  
  13. loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));  
  14. this->addChild(loadBg,1);  
  15.   
  16. loadProgress = ProgressTimer::create(Sprite::create("sliderProgress.png"));//创建一个进程条  
  17. loadProgress->setBarChangeRate(Point(1,0));//设置进程条的变化速率  
  18. loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型  
  19. loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向  
  20. loadProgress->setPosition(Point(visibleSize.width/2,visibleSize.height/2));  
  21. loadProgress->setPercentage(0.0f);//设置初始值为0  
  22. this->addChild(loadProgress,2);  

2、加载图片
  1. //加载20张图片,每加载完一张就调用回调函数:loadingCallback  
  2. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  3. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld1.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  4. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld2.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  5. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld3.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  6. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld4.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  7. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld5.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  8. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld6.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  9. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld7.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  10. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  11. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  12.   
  13. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  14. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  15. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  16. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  17. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  18. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  19. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  20. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  21. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  
  22. Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));  

3、图片加载后的回调函数:
  1. void HelloWorld::loadingCallback(Object* pSender)  
  2. {  
  3.     ++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1  
  4.   
  5.     char buf_str[16];  
  6.     sprintf(buf_str,"%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp);  
  7.     percentLabel->setString(buf_str);//更新percentLabel的值  
  8.   
  9.     float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp)/((float)m_numSp/100);//计算进度条当前的百分比  
  10.     //因为加载图片速度很快,所以就没有使用ProgressTo,  
  11.     //或者ProgressFromTo这种动作来更新进度条  
  12.     loadProgress->setPercentage(newPercent);//更新进度条  
  13.   
  14.     //图片加载完成后  
  15.     if(m_loadedSp == m_numSp)  
  16.     {  
  17.         this->removeChild(loadProgress);//将添加的几个对象删除掉  
  18.         this->removeChild(percentLabel);  
  19.         this->removeChild(loadLabel);  
  20.   
  21.         //加载完既要跳转到gotoNewLayer,在这里可以  
  22.         //创建新的Scene,新的Layer,或者其他什么乱七八糟的  
  23.         this->gotoNewLayer();  
  24.     }  
  25. }  

4、进入新的界面
  1. void HelloWorld::gotoNewLayer()  
  2. {  
  3.     auto size = Director::getInstance()->getWinSize();  
  4.   
  5.     auto sp = Sprite::create("HelloWorld.png");//用之前加载到缓存中的图片,创建一个精灵。  
  6.     sp->setPosition(Point(size.width/2,size.height/2));  
  7.     this->addChild(sp,1);  
  8. }  


因为代码里注释都写的挺详细的,所以我也就不说太多废话了。