访问我的个人博客 秋码个人博客
先看实现的效果图
首先在头文件添加
#include "ui/CocosGUI.h"
在init方法编辑相应的代码
添加进度背景图
auto loadbg = Sprite::create("jindutiaoBG.png");
loadbg->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y - 200));
this->addChild(loadbg,2);
在者创建LoadingBar 使用schedule调度器控制进度速度
auto lb = ui::LoadingBar::create("jindutiao.png");
lb->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y-200));
lb->setDirection(ui::LoadingBar::Direction::LEFT);
lb->setPercent(0);
addChild(lb, 3, 3);//同时设置Tag值为1
schedule(CC_CALLBACK_1(Loading::LoadingBar, this), "LoadingBar");
Loading::count = 0;
//加载进度条的进度
void Loading::LoadingBar(float dt){
count++;
CCLOG("ccccccccccc");
auto size = Director::getInstance()->getVisibleSize();
auto lb = dynamic_cast<class ui::LoadingBar*>(getChildByTag(3));
//float per = lb->getPercent() + 1.0f;
float per = count / 12 * 100;
lb->setPercent(per);
switch (count){
case 2:
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("data/music/bg/a_chooselevel.mp3");
break;
case 12:
unschedule("LoadingBar");
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("data/music/bg/a_yueyemusic.mp3");
std::string docpath=FileUtils::getInstance()->getWritablePath() + "hero.txt";
//判断文件是否存在
if (!FileUtils::getInstance()->isFileExist(docpath)){
//不存在则到这个场景
CCLOG("aaaaaaaaaaaaa");
auto callf = CallFunc::create([this](){
//Director::getInstance()->replaceScene(StartLayer::createScene());
Director::getInstance()->replaceScene(TransitionPageTurn::create(2, StartLayer::createScene(), false));
});
auto seq = Sequence::create(DelayTime::create(1.1), callf, NULL);
this->runAction(seq);
}
else{
CCLOG("bbbbbbbbbbb");
auto callf = CallFunc::create([this](){
Director::getInstance()->replaceScene(TransitionPageTurn::create(2, MainLayer::createScene(), false));
});
auto seq = Sequence::create(DelayTime::create(1.1), callf, NULL);
this->runAction(seq);
}
break;
}
}