//
// ResourceReload.hpp
// HelloCocos
//
// Created by LXBig on 17/5/8.
//
//
#ifndef ResourceReload_hpp
#define ResourceReload_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
#include "cocos2d.h"
USING_NS_CC;
class ReloadeResouce:public Layer {
public:
static Scene* createScene();
virtual bool init();
CREATE_FUNC(ReloadeResouce);
void loadingCallback(Ref* pSender);//加载一张图片完成后跳转的回调函数
void gotoNewLayer();//加载完后的跳转函数
private:
cocos2d::ProgressTimer* loadProgress;//进度条
Label* percentLabel;//加载进度label
Label* loadLabel;//显示 loading: 的label
int m_numSp;//要加载的资源数目
int m_loadedSp;//一经加载的资源数目
};
#endif /* ResourceReload_hpp */
//
// ResourceReload.cpp
// HelloCocos
//
// Created by LXBig on 17/5/8.
//
//
#include "ResourceReload.hpp"
Scene* ReloadeResouce::createScene()
{
auto scene = Scene::create();
auto layer = ReloadeResouce::create();
scene->addChild(layer);
return scene;
}
bool ReloadeResouce::init()
{
if (!Layer::init()) {
return false;
}
m_numSp = 100;
m_loadedSp = 0;
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
loadLabel = Label::createWithSystemFont("Loading:","Arial",20);//创建显示Loading: 的label
loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));
this->addChild(loadLabel,10);
percentLabel = Label::createWithSystemFont("0%","Arial",20);//创建显示百分比的label
percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));
this->addChild(percentLabel,2);
auto loadBg = Sprite::create("Back.png");//进程条的底图
loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(loadBg,1);
loadProgress = ProgressTimer::create(Sprite::create("loadingbar1.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));
this->addChild(loadProgress,100);
//加载一百次图片
for (int i = 1; i <= m_numSp; i++) {
// char str[30];
// sprintf(str, "shop_lan%d.png",i);
// cout<<str<<endl;
//纹理缓冲,addImageAsync函数是加载一次调用一次函数
Director::getInstance()->getTextureCache()->addImageAsync("haha.jpg", CC_CALLBACK_1(ReloadeResouce::loadingCallback, this));
}
return true;
}
void ReloadeResouce::loadingCallback(Ref* 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 = ((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();
}
}
void ReloadeResouce::gotoNewLayer()
{
auto size = Director::getInstance()->getWinSize();
auto sp = Sprite::create("haha.jpg");//用之前加载到缓存中的图片,创建一个精灵。
sp->setPosition(Point(size.width/2,size.height/2 - 80));
this->addChild(sp,1);
}