参照人家的教程将loading进度条自我实现了一下
一、在HelloWorld.h中代码
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
HelloWorld() :m_numSp(1000), m_loadedSp(0), loadProgress(NULL){};
void loadingCallbak(Ref* pSender);//加载一张图片完成后跳转到更新函数
void nextLayer();
ProgressTimer* loadProgress;//进度条
LabelTTF* precentLabel;//加载进度label
LabelTTF* loadLabel;//显示loading的label
int m_numSp;//要加载的精灵数目,初始化为 10 张
int m_loadedSp;//已加载的精灵数目
};
#endif // __HELLOWORLD_SCENE_H__
二、在HelloWorld.cpp中的代码
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
loadLabel = LabelTTF::create("Loading","Arial",20);
loadLabel->setPosition(visibleSize.width * 0.5,visibleSize.height * 0.7);
this->addChild(loadLabel);
precentLabel = LabelTTF::create("0%","Arial",20);
precentLabel->setPosition(visibleSize.width * 0.5,visibleSize.height * 0.4);
this->addChild(precentLabel);
//add bg
//auto bg = Sprite::create("bg.jpg");
//bg->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5);
//this->addChild(bg);
//add loadbg
auto loadBg = Sprite::create("Loading1.png");
//loadBg->setAnchorPoint(Point(0, 0.5));
loadBg->setPosition(visibleSize.width * 0.5,visibleSize.height * 0.5);
this->addChild(loadBg);
loadProgress = ProgressTimer::create(Sprite::create("Loading2.png"));//创建一个进程条
loadProgress->setBarChangeRate(Vec2(1,0));//设置进程条的变化速率
loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型
loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向
loadProgress->setPosition(visibleSize.width * 0.5,visibleSize.height * 0.5);
loadProgress->setPercentage(0.0f);//设置初始值为0
this->addChild(loadProgress);
for (int i = 0; i < m_numSp;i ++)
{
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png", CC_CALLBACK_1(HelloWorld::loadingCallbak,this));//异步加载相关资源 关键性的
}
return true;
}
void HelloWorld::loadingCallbak(Ref* pSender){
++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1
char buff[16];
sprintf(buff, "%d%%", (int)(((float)m_loadedSp / m_numSp) * 100), m_numSp);
precentLabel->setString(buff);//更新percentLabel的值
float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp) / ((float)m_numSp / 100);//计算进度条当前的百分比
//float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp) / ((float)m_numSp / 100);//计算进度条当前的百分比
loadProgress->setPercentage(newPercent);//更新进度条
//图片加载完成后
if (m_loadedSp == m_numSp)
{
this->removeChild(loadProgress);//将添加的几个对象删除掉
this->removeChild(precentLabel);
this->removeChild(loadLabel);
//加载完既要跳转到gotoNewLayer,在这里可以
//创建新的Scene,新的Layer,或者其他什么乱七八糟的
this->nextLayer();
}
}
void HelloWorld::nextLayer(){
log("1312233");
}
三、实现的最终效果图
代码下载:http://download.csdn.net/detail/u010983974/7794463