1. 程式人生 > >cocos-2d-x中進度條(loadingBar)顯示資源載入

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<cocos2d::ui::LoadingBar*>(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<cocos2d::ui::LoadingBar*>(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());
    }
}  // 真檢測資源更新