Hi,
I have a “LoadingScene” where I preload resources (images, sounds…). I want to put a LoadingBar animation but it only runs where all resources have been loaded. I want to make it runs WHILE the resources are being loading.
This is the code:
#include "LoadingScene.h"
#include "MainMenuScene.h"
#include "AppManager.h"
USING_NS_CC;
Scene* LoadingScene::createScene()
{
auto scene = Scene::create();
auto layer = LoadingScene::create();
scene->addChild(layer);
return scene;
}
bool LoadingScene::init()
{
if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255)) )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(BACKGROUNDMUSIC, true);
manager = AppManager::sharedManager();
_count = 0;
loadingBar = cocos2d::ui::LoadingBar::create("sliderProgress.png", _count);
loadingBar->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
loadingBar->setDirection(LoadingBar::Direction::LEFT);
this->addChild(loadingBar, 1);
this->scheduleUpdate();
Sequence *sequence = Sequence::create(CallFunc::create(CC_CALLBACK_0(LoadingScene::preload, this)), CallFunc::create(CC_CALLBACK_0(LoadingScene::loadScene, this)), NULL);
this->runAction(sequence);
return true;
}
void LoadingScene::preload(){
manager->copyDatabase();
manager->preloadSlider();
manager->preloadSprites();
manager->preloadSounds();
}
void LoadingScene::update(float dt)
{
_count++;
if (_count > 100)
{
_count = 0;
}
loadingBar->setPercent(_count);
}
void LoadingScene::loadScene(){
auto homeScene = MainMenu::createScene();
Director::getInstance()->replaceScene(CCTransitionFade::create(0.7, homeScene, Color3B::WHITE));
}
Thank you!