cocos2dx 加载 进度条样式首先,我要说的是:这次我要写的主题是进度条。 额,等等,先收起你手里愤怒的西瓜刀。我也才知道TestCpp也有这个例子啊。不过TestCpp里的只有label的变化,而我的多加了个进度条。
恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。
1、创建
2、加载图片
3、图片加载后的回调函数:
4、进入新的界面
因为代码里注释都写的挺详细的,所以我也就不说太多废话了。
恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。
首先看下头文件:HelloWorld.h
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- class HelloWorld : public cocos2d::Layer
- {
- public:
- HelloWorld():m_numSp(20),m_loadedSp(0),loadProgress(NULL){};
- static cocos2d::Scene* createScene();
- virtual bool init();
- void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数
- void gotoNewLayer();//加载完后的跳转函数
- CREATE_FUNC(HelloWorld);
- private:
- cocos2d::ProgressTimer* loadProgress;//进度条
- cocos2d::LabelTTF* percentLabel;//加载进度label
- cocos2d::LabelTTF* loadLabel;//显示 loading: 的label
- int m_numSp;//要加载的精灵数目,初始化为 20 张
- int m_loadedSp;//已加载的精灵数目
- };
- #endif // __HELLOWORLD_SCENE_H__
1、创建
- Size visibleSize = Director::getInstance()->getVisibleSize();
- Point origin = Director::getInstance()->getVisibleOrigin();
- loadLabel = LabelTTF::create("Loading:","Arial",20);//创建显示Loading: 的label
- loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));
- this->addChild(loadLabel,1);
- percentLabel = LabelTTF::create("0%","Arial",20);//创建显示百分比的label
- percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));
- this->addChild(percentLabel,2);
- auto loadBg = Sprite::create("sliderTrack.png");//进程条的底图
- loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
- this->addChild(loadBg,1);
- loadProgress = ProgressTimer::create(Sprite::create("sliderProgress.png"));//创建一个进程条
- loadProgress->setBarChangeRate(Point(1,0));//设置进程条的变化速率
- loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型
- loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向
- loadProgress->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
- loadProgress->setPercentage(0.0f);//设置初始值为0
- this->addChild(loadProgress,2);
2、加载图片
- //加载20张图片,每加载完一张就调用回调函数:loadingCallback
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld1.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld2.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld3.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld4.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld5.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld6.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld7.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
- Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
3、图片加载后的回调函数:
- void HelloWorld::loadingCallback(Object* pSender)
- {
- ++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1
- char buf_str[16];
- sprintf(buf_str,"%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp);
- percentLabel->setString(buf_str);//更新percentLabel的值
- float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp)/((float)m_numSp/100);//计算进度条当前的百分比
- //因为加载图片速度很快,所以就没有使用ProgressTo,
- //或者ProgressFromTo这种动作来更新进度条
- loadProgress->setPercentage(newPercent);//更新进度条
- //图片加载完成后
- if(m_loadedSp == m_numSp)
- {
- this->removeChild(loadProgress);//将添加的几个对象删除掉
- this->removeChild(percentLabel);
- this->removeChild(loadLabel);
- //加载完既要跳转到gotoNewLayer,在这里可以
- //创建新的Scene,新的Layer,或者其他什么乱七八糟的
- this->gotoNewLayer();
- }
- }
4、进入新的界面
- void HelloWorld::gotoNewLayer()
- {
- auto size = Director::getInstance()->getWinSize();
- auto sp = Sprite::create("HelloWorld.png");//用之前加载到缓存中的图片,创建一个精灵。
- sp->setPosition(Point(size.width/2,size.height/2));
- this->addChild(sp,1);
- }
因为代码里注释都写的挺详细的,所以我也就不说太多废话了。