您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x 3.10 资源加载进度条Loading显示百分比

2017-01-09 11:14 447 查看
参考:http://blog.csdn.net/star530/article/details/19420317

最近在学习Cocos2dx 练习的过程 简单记录一下 参考博文在上边

转载请注明!

本文比较适合刚接触cocos2dx的朋友,简单易懂,话不多说 下边开始!

步骤:

1.创建Label和ProgressTimer

2.加载资源,每加载一张就调用回调函数

3.加载完成进入新的界面
首先创建一个Loading界面,Loading.h头文件如下:

#include "cocos2d.h"

using namespace cocos2d;

class Loading :public Layer{

public:

Loading() :m_numSp(81), m_loadedSp(0), loadProgress(NULL){};

static Scene* createScene();

virtual bool init();

void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数

void gotoNewLayer();//加载完后的跳转函数

CREATE_FUNC(Loading);

private:

ProgressTimer* loadProgress;//进度条

LabelTTF* percentLabel;//加载进度label

LabelTTF* loadLabel;//显示 loading: 的label

int m_numSp;//要加载的精灵数目,初始化为 81 张

int m_loadedSp;//已加载的精灵数目

};

创建Loading.cpp并实现:

1.在init()方法里创建

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);

percentLabel = LabelTTF::create("0%", "Arial", 20);//创建显示百分比的label

percentLabel->setPosition(Point(visibleSize.width / 2 + 35, visibleSize.height / 2 + 30));

this->addChild(percentLabel);

auto loadBg = Sprite::create("slider_bar.png");//进程条的底图

loadBg->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));

this->addChild(loadBg);

loadProgress = ProgressTimer::create(Sprite::create("silder_progressBar.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.加载资源,还是在init()方法里(ps:原文使用的是清晰易懂方法,这里我自己进化了一下)

//加载81张图片,每加载完一张就调用回调函数:loadingCallback

for (int i = 7001; i < 7082; i++){

std::string imgName = StringUtils::format("%d.png", i);

Director::getInstance()->getTextureCache()->addImageAsync(imgName, CC_CALLBACK_1(Loading::loadingCallback, this));

}

3.图片加载后的回调函数实现如下:

void Loading::loadingCallback(Object* pSender){

++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1

char buf_str[16];

sprintf(buf_str, "%d%%", (int)(((float)m_loadedSp / m_numSp) * 100));

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 Loading::gotoNewLayer(){

auto size = Director::getInstance()->getWinSize();

auto sp = Sprite::create("7069.png");//用之前加载到缓存中的图片,创建一个精灵

sp->setPosition(Point(size.width / 2, size.height / 2));

this->addChild(sp, 1);

}

以上代码需注意以下几点总结:

1.Loading.h头文件中声明的变量m_numSp一定要初始化

2.Loading.cpp中加载资源的地方需要注意的是与参考文章略有不同1.循环文件名;2.3.0与3.10回调函数的区别!!!

3.循环读取资源的时候 一定要记得修改初始化的默认资源数量 否则会报错!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息