Cocos2dx异步加载资源

LoadScene.h

#ifndef __LOADING_SCENE_H__

#define __LOADING_SCENE_H__

 

#include "cocos2d.h"

USING_NS_CC;

using namespace std;

class LoadingScene : public Layer

{

public:

LoadingScene();

~LoadingScene();

virtual bool init();

static Scene* createScene();

CREATE_FUNC(LoadingScene);

void loadImage();

void updateProgress(float delta);

private:

ProgressTimer* loadProgress;

Label* percentLb;

int _numOfLodingFiles;

bool _loadEnd;

};

 

#endif

LoadingScene.cpp

#include "LoadingScene.h"

#include "Define.h"

#include "HelloWorldScene.h"

LoadingScene::LoadingScene()

{

 

}

LoadingScene::~LoadingScene()

{

 

}

Scene* LoadingScene::createScene()

{

auto scene = Scene::create();

auto layer = LoadingScene::create();

layer->setPosition(RELATIVEPOS);

scene->addChild(layer);

return scene;

}

 

 

bool LoadingScene::init()

{

if (!Layer::init())

return false;

 

//进度底纹;

auto progressShake = Sprite::create("img_loading_jdt_bg.png");

progressShake->setPosition(Vec2(480,320));

this->addChild(progressShake);

 

//加载进度;

auto sp = Sprite::create("img_loading_jdt_ls.png");

loadProgress = ProgressTimer::create(sp);//创建进度条

loadProgress->setBarChangeRate(Vec2(1, 0));//进度条变化的速率

loadProgress->setType(ProgressTimer::Type::BAR);

loadProgress->setMidpoint(Vec2(0, 1));

loadProgress->setPosition(Vec2(480,320));

loadProgress->setPercentage(0.0f);

this->addChild(loadProgress);

 

//进度特效;

auto effect = Sprite::create("img_loading_jdt_guang.png");

effect->setAnchorPoint(Vec2(0, 0.5));

effect->setTag(10);

loadProgress->addChild(effect);

effect->setPosition(loadProgress->getPercentage() * (loadProgress->getContentSize().width / 100.0) - 40,

loadProgress->getContentSize().height / 2);

 

//进度条底层图片;

auto progressPic = Sprite::create("img_loading_jdt.png");

progressPic->setPosition(Vec2(480,320));

this->addChild(progressPic);

 

 

//进度比;

percentLb = Label::create();

percentLb->setString(StringUtils::format("%d", 0));

percentLb->setPosition(progressPic->getContentSize().width - 50, progressPic->getContentSize().height / 2);

progressPic->addChild(percentLb);

 

_numOfLodingFiles = 0;

_loadEnd = false;

 

this->scheduleOnce([=](float){

LoadingScene::loadImage();

}, 0.5f, "loadimage");

 

this->schedule(schedule_selector(LoadingScene::updateProgress), 0.1f);

 

return true;

}

 

void LoadingScene::loadImage()//异步加载资源;

{

std:vector<string> paths;//以下都是plist文件;

paths.push_back("abc_1");

paths.push_back("abc_1_1");

paths.push_back("abc_2");

paths.push_back("abc_3");

paths.push_back("abc_3_1");

paths.push_back("abc_4");

 

for (int i = 0; i < paths.size(); i++){

Director::getInstance()->getTextureCache()->addImageAsync(paths[i] + ".png", [=](Texture2D*){

if (i < paths.size()){

SpriteFrameCache::getInstance()->addSpriteFramesWithFile(paths[i] + ".plist");

}

_numOfLodingFiles++;

if (_numOfLodingFiles == paths.size()){

_loadEnd = true;

}

});

}

}

 

void LoadingScene::updateProgress(float delta)

{

float progressPercent = loadProgress->getPercentage();

Sprite* sp = (Sprite*)loadProgress->getChildByTag(10);

 

if (_loadEnd){

progressPercent += 5.0f;

}

else{

progressPercent += 0.6f;

}

if (progressPercent >= 95){

progressPercent = 95;

}

 

loadProgress->setPercentage(progressPercent);

percentLb->setString(StringUtils::format("%d%%", (int)loadProgress->getPercentage()));

sp->setPositionX(loadProgress->getPercentage() * (loadProgress->getContentSize().width / 100.0) - 40);

 

if (progressPercent >= 95 && _loadEnd){

this->unschedule(schedule_selector(LoadingScene::updateProgress));

auto scene = HelloWorld::createScene();

Director::getInstance()->replaceScene(scene);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值