配置文件:battleRes.json
{ "png": [ "sprite/heroSpecialEffect1.png", "sprite/heroSpecialEffect2.png", "sprite/planeLine.png", "sprite/sky_bg.png", "sprite/superReward1.png", "sprite/superReward2.png", "BG/city/chengshi1.png", "BG/city/chengshi1a.png", "BG/city/chengshi2.png", "BG/city/chengshi3.png", "BG/highway/gongludi1.png", "BG/highway/gongludi2.png", "BG/highway/gongludi3.png", "BG/park/gongyuan2.png", "BG/park/gongyuan3.png", "BG/park/gongyuan6a.png", "BG/park/gongyuan6b.png" ], "plist": [ [ "sprite/effects.png", "sprite/effects.plist" ] ], "armature": [ "HeroAnimation/boy.ExportJson", "HeroAnimation/boy_loading.ExportJson" ] }
LoadingLayer.h
#include "BaseLayer.h" #include "BaseLayer.h" #include "cocostudio/CocoStudio.h" #include "ui/CocosGUI.h" #include <queue> class LoadingLayer : public BaseLayer { public: CREATE_FUNC(LoadingLayer); virtual bool init(); virtual void onClickMenu() {}; virtual void onClickOK() {}; void setConfig(const char *fileName); void start(); CC_CONSTRUCTOR_ACCESS: LoadingLayer(); virtual ~LoadingLayer(); private: void myUpdate(float delay); void imageAsyncCallback(cocos2d::Texture2D *texture); void plistImageAsyncCallback(cocos2d::Texture2D *texture); void jsonAsyncCallback(float f); void doLoad(); public: typedef std::function<void()> LoadingCallback; LoadingCallback onBegan; LoadingCallback onUpdate; LoadingCallback onFinish; private: cocos2d::ui::ImageView *m_bg; cocos2d::ui::Text *m_loadingNum; rapidjson::Document loadJson; struct plistStruct { std::string plist; std::string png; }; std::queue<std::string> pngQueue; std::queue<plistStruct> plistQueue; std::queue<std::string> armatureQueue; int totalCount; int count; float percent; float showPercent; }; #endif
LoadingLayer.cpp
#include "LoadingLayer.h" #include "MyPath.h" #include "GameManager.h" #include "LsTools.h" USING_NS_CC; USING_NS_GUI; LoadingLayer::LoadingLayer() : onBegan(nullptr), onFinish(nullptr), onUpdate(nullptr), percent(0.0f), showPercent(0.0f), totalCount(0), count(0) { } LoadingLayer::~LoadingLayer() { } bool LoadingLayer::init() { if (!Layer::init()) { return false; } _rootNode = CSLoader::createNode("LoadingLayer.csb"); addChild(_rootNode); m_bg = (ImageView *)Helper::seekWidgetByName((Widget *)_rootNode, "bg"); m_loadingNum = (Text *)Helper::seekWidgetByName((Widget *)_rootNode, "loadingnumber_Lab"); m_loadingNum->setString(Value(showPercent).asString()); int temp = random(1, 2); temp = 1; m_bg->loadTexture(StringUtils::format(MyPath::getInstance()->getPATH_loadingBG_png().c_str(), temp)); this->setSwallowEnable(true); Director::getInstance()->getTextureCache()->removeUnusedTextures(); SpriteFrameCache::getInstance()->removeUnusedSpriteFrames(); return true; } void LoadingLayer::setConfig(const char *fileName) { LsTools::readJsonWithFile(fileName, loadJson); } void LoadingLayer::start() { for (unsigned int i = 0; i < loadJson["png"].Size(); i++) pngQueue.push(loadJson["png"][i].GetString()); for (unsigned int i = 0; i < loadJson["plist"].Size(); i++) { plistStruct res; res.png = loadJson["plist"][i][0].GetString(); res.plist = loadJson["plist"][i][1].GetString(); plistQueue.push(res); } for (unsigned int i = 0; i < loadJson["armature"].Size(); i++) armatureQueue.push(loadJson["armature"][i].GetString()); if (onBegan) onBegan(); totalCount = pngQueue.size() + plistQueue.size() + armatureQueue.size(); count = 0; doLoad(); schedule(CC_SCHEDULE_SELECTOR(LoadingLayer::myUpdate), 0.01f); } void LoadingLayer::doLoad() { if (pngQueue.size() > 0) { Director::getInstance()->getTextureCache()->addImageAsync(pngQueue.front(), CC_CALLBACK_1(LoadingLayer::imageAsyncCallback, this)); } else if (plistQueue.size() > 0) { Director::getInstance()->getTextureCache()->addImageAsync(plistQueue.front().png, CC_CALLBACK_1(LoadingLayer::plistImageAsyncCallback, this)); } else if (armatureQueue.size() > 0) { ArmatureDataManager::getInstance()->addArmatureFileInfoAsync(armatureQueue.front(), this, schedule_selector(LoadingLayer::jsonAsyncCallback)); } count++; } void LoadingLayer::imageAsyncCallback(cocos2d::Texture2D *texture) { pngQueue.pop(); doLoad(); } void LoadingLayer::plistImageAsyncCallback(cocos2d::Texture2D *texture) { SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistQueue.front().plist, texture); plistQueue.pop(); doLoad(); } void LoadingLayer::jsonAsyncCallback(float f) { armatureQueue.pop(); doLoad(); } void LoadingLayer::myUpdate(float delay) { percent = (float)count / totalCount * 100; if (count > totalCount && showPercent >= 100.0f) { unschedule(CC_SCHEDULE_SELECTOR(LoadingLayer::myUpdate)); if (onFinish) onFinish(); close(); return; } if (showPercent > 100.0f) { showPercent = 100.0f; } else if (showPercent < percent) { showPercent++; } m_loadingNum->setString(Value((int)showPercent).asString()); CCLOG("%d %d", count, totalCount); CCLOG("showPercent %f", showPercent); }