cocos-2d-x中进度条(loadingBar)显示资源加载

为便于看客实际操作和理解,直接上例子

#include "LoadingBar.h"
#include "HelloWorldScene.h"
#include "GameScene.h"
static int res_count=0;
//  资源集合
std::string PRELOAD_PLIST[]=
{
    "mainscene1-hd",
    "stages_theme2-hd",
    "themescene3-hd",
    "TBottle-hd",
    "TBlueStar-hd",
    "TFireBottle-hd",
    "stages_theme5-hd",
    "themescene2-hd",
    "stages_bg",
    "Items02-hd",
    "gamemenu-hd",
    "Items01-hd",
    "gameover-hd",
    "gameover0-hd",
    "TRocket-hd"
};
//提示语集合
std::string TIPS_LIST[]=
{
    "hello",
    "true",
    "love",
    "stories",
    "never",
    "have",
    "endings",
};


Scene* BarBar::createScene()
{
    auto scene=Scene::create();
    auto layer=BarBar::create();
    scene->addChild(layer);
    return scene;
}

bool BarBar::init()
{
    if (!Layer::init())
    {
        return false;
    }
    
    // 添加背景
    auto bg = Sprite::create("mainbg1.png");
    bg->setAnchorPoint(Vec2(0,0));
    bg->setPosition(Vec2::ZERO);
    addChild(bg,-100);
    // 进度条背景
    auto barbg=Sprite::create("barbg.png");
    barbg->setPosition(VISIBLESIZE.width/2,VISIBLESIZE.height/4);
    barbg->setTag(2);
    addChild(barbg,-99);
    
    // 进度数
    auto BarLabel=Label::createWithTTF("aa", "fonts/Marker Felt.ttf", 30);
    BarLabel->setString(StringUtils::format("%d",barp));
    BarLabel->setColor(Color3B::RED);
    BarLabel->setAnchorPoint(Vec2(1, 0.5));
    BarLabel->setTag(450);
    addChild(BarLabel,100);
    BarLabel->setPosition(Vec2(505,100));
    auto BarLabel1=Label::createWithTTF("%", "fonts/Marker Felt.ttf", 30);
    BarLabel1->setColor(Color3B::RED);
    BarLabel1->setPosition(Vec2(520,100));
    addChild(BarLabel1);
    
    // 小提示
    auto tips=Label::createWithTTF(TIPS_LIST[rand()%7], "fonts/Marker Felt.ttf", 30);
    tips->setTag(9999);
    tips->setColor(Color3B::RED);
    tips->setPosition(Vec2(VISIBLESIZE.width/2, VISIBLESIZE.height/4+60));
    schedule(CC_CALLBACK_0(BarBar::changestring, this), 0.2f,"tips");
    addChild(tips);
    
    
   
    
    // 进度条
    auto bar=ui::LoadingBar::create("bar.png");
    bar->setTag(1);
    barbg->addChild(bar,99);
    bar->setAnchorPoint(Vec2(0,0.5));
    bar->setPosition(Vec2(0, barbg->getContentSize().height/2));
    bar->setDirection(cocos2d::ui::LoadingBar::Direction::LEFT);
    bar->setPercent(0);
//    this->schedule(CC_CALLBACK_1(::BarBar::loadingBarUpdate, this), 0.1,"loadingBarUpdate");
    loadResources();
    
    return true;
}
void  BarBar::changestring()
{
    auto tips=(Label*)this->getChildByTag(9999);
    tips->setString(TIPS_LIST[rand()%7]);//随机显示提示语
}

 
  

void BarBar::loadingBarUpdate(float dt){
    auto lb=dynamic_cast(getChildByTag(2)->getChildByTag(1)); //获取进度条
    float percent=lb->getPercent()+1;   //更新进度
    lb->setPercent(percent);
    auto moneyLabel=(Label*)getChildByTag(2)->getChildByTag(450);
    barp+=1;
    moneyLabel->setString(StringUtils::format("%d",barp));
    if (percent>100) {
        lb->setPercent(100);
        unschedule("loadingBarUpdate");
        Director::getInstance()->replaceScene(HelloWorld::createScene());
    }
}  //假的资源加载进度条

void BarBar::loadResources(){
    //加载资源 
    auto cache=Director::getInstance()->getTextureCache();//缓存
    //  资源异步加载
    cache->addImageAsync("mainscene1-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("stages_theme2-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("themescene3-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("TBottle-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("TBlueStar-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("TFireBottle-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("stages_theme5-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("themescene2-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("stages_bg.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("Items02-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("gamemenu-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("Items01-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("gameover-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("gameover0-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
    cache->addImageAsync("TRocket-hd.png",CC_CALLBACK_1(BarBar::loadResourcesUpdate, this));
}  //真资源加载

void BarBar::loadResourcesUpdate(Texture2D*){
     SpriteFrameCache::getInstance()->addSpriteFramesWithFile(PRELOAD_PLIST[res_count]+(".plist"),PRELOAD_PLIST[res_count]+(".png"));
    // 更新进度条
    res_count++;
    float percent=100*res_count/15.0;//一共需要加载15次(因为有15个文件)
    auto lb=dynamic_cast(getChildByTag(2)->getChildByTag(1));
    lb->setPercent(percent);
    // 更新进度数
    auto BarLevel=(Label*)getChildByTag(450);
    barp=percent;
    BarLevel->setString(StringUtils::format("%d",barp));
    //加载完成,切换界面
    if (percent>=100) {
       Director::getInstance()->replaceScene(HelloWorld::createScene());
//        Director::getInstance()->replaceScene(GameScene::createScene());
    }
}  // 真检测资源更新


 
  
 
  
 
  
 
 

你可能感兴趣的:(cocos2d-x)