Cocos2d-x 3.x 异步加载资源和Loading界面
2015-09-22 15:41
393 查看
一个异步加载图片资源和动画资源的ladoing界面。下面给代码
.h文件
.cpp
.h文件
#ifndef RUN_SCENE_LOADINGLAYER_H__ #define RUN_SCENE_LOADINGLAYER_H__ #include "cocos2d.h" using namespace cocos2d; class LoadingLayer : public Layer { public: <span style="white-space:pre"> </span>LoadingLayer(); <span style="white-space:pre"> </span>~LoadingLayer(); <span style="white-space:pre"> </span>virtual bool init(); <span style="white-space:pre"> </span>CREATE_FUNC(LoadingLayer); <span style="white-space:pre"> </span>virtual void onEnter(); <span style="white-space:pre"> </span>virtual void update(float dt); <span style="white-space:pre"> </span>void AddImageCallBack(Texture2D *texture);<span style="white-space:pre"> </span>//加图片 <span style="white-space:pre"> </span>void AddAniCallBack(float dt);<span style="white-space:pre"> </span>//加动画 protected: <span style="white-space:pre"> </span>std::vector<std::string> m_stvImageNames;<span style="white-space:pre"> </span>//所有要加载的图片名称 <span style="white-space:pre"> </span>std::vector<std::string> m_stvAniNames;<span style="white-space:pre"> </span>//所有要加载的动画名称 <span style="white-space:pre"> </span>std::vector<std::string> m_stvSpriteFrameName;<span style="white-space:pre"> </span>//所有要加载的大图名称 <span style="white-space:pre"> </span>ProgressTimer* m_pLoadingProress;<span style="white-space:pre"> </span>//进度条 <span style="white-space:pre"> </span>Label* m_pLoadingLabel; <span style="white-space:pre"> </span>int m_iLoadingNum; <span style="white-space:pre"> </span>float m_fPassTime; <span style="white-space:pre"> </span>bool m_bImageHasLoading; }; #endif // LOADINGLAYER_H__
.cpp
#include "LoadingLayer.h" #include "GameScene.h" #include "data\game_constant_defines.h" LoadingLayer::LoadingLayer() : <span style="white-space:pre"> </span>m_pLoadingProress(nullptr), <span style="white-space:pre"> </span>m_fPassTime(0.0f), <span style="white-space:pre"> </span>m_bImageHasLoading(false) { <span style="white-space:pre"> </span>m_pLoadingLabel = nullptr; <span style="white-space:pre"> </span>m_iLoadingNum = 0; <span style="white-space:pre"> </span>m_stvImageNames.clear(); <span style="white-space:pre"> </span>m_stvAniNames.clear(); <span style="white-space:pre"> </span>m_stvSpriteFrameName.clear(); } LoadingLayer::~LoadingLayer() { } bool LoadingLayer::init() { <span style="white-space:pre"> </span>if (!Layer::init()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>const char* imageNames[] = {"map/map_0.png", "map/map_1.png", "map/map_2.png", "map/map_3.png", "map/map_4.png", "map/mist.png", <span style="white-space:pre"> </span>"map/moon.png", "map/cloud.png", "ziy/cd.png", "ziy/dpadDir.png","ziy/dpadmove.png", "ziy/LL_E.png", "ziy/LL_Q.png", "ziy/LL_R.png", <span style="white-space:pre"> </span>"ziy/LL_shanxian.png", "ziy/LL_touxiang.png","ziy/touchAttack.png", "1001_2.png", "1006_3.png", "1009_1.png", "selcet_hero_redcircle.png", "vac.png" }; <span style="white-space:pre"> </span>for (int i = 0; i != sizeof(imageNames) / sizeof(imageNames[0]); ++i) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>m_stvImageNames.push_back(imageNames[i]); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>const char* aniNames[] = { "ani/ll_bea/ll_bea.ExportJson", "ani/ll_missile/ll_missile.ExportJson", "ani/ll/ll.ExportJson", "ani/chui/chui.ExportJson", <span style="white-space:pre"> </span>"ani/public/shanxian.ExportJson", "ani/fa/fa.ExportJson", "ani/fa_missile/fa_missile.ExportJson", "ani/buff/buff.ExportJson", "ani/pao/pao.ExportJson", <span style="white-space:pre"> </span>"ani/pao_missile/pao_missile.ExportJson", "ani/shu1/shu1.ExportJson" }; <span style="white-space:pre"> </span>for (int i = 0; i != sizeof(aniNames) / sizeof(aniNames[0]); ++i) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>m_stvAniNames.push_back(aniNames[i]); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>//const char* frameNames[] = { "ani/ll/ll0", "ani/chui/chui0" }; <span style="white-space:pre"> </span>//for (int i = 0; i != sizeof(frameNames) / sizeof(frameNames[0]); ++i) <span style="white-space:pre"> </span>//{ <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>m_stvSpriteFrameName.push_back(frameNames[i]); <span style="white-space:pre"> </span>//} <span style="white-space:pre"> </span>////先加入plist <span style="white-space:pre"> </span>//for (int i = 0; i != m_stvSpriteFrameName.size(); ++i) <span style="white-space:pre"> </span>//{ <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>std::string tempString = m_stvSpriteFrameName[i]; <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>SpriteFrameCache::getInstance()->addSpriteFramesWithFile(m_stvSpriteFrameName[i].append(".plist")); <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>m_stvSpriteFrameName[i] = tempString; <span style="white-space:pre"> </span>//} <span style="white-space:pre"> </span>////再把png放入待加载容器 <span style="white-space:pre"> </span>//for (int i = 0; i != m_stvSpriteFrameName.size(); ++i) <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>m_stvImageNames.push_back(m_stvSpriteFrameName[i].append(".png")); <span style="white-space:pre"> </span>return true; } void LoadingLayer::onEnter() { <span style="white-space:pre"> </span>Layer::onEnter(); <span style="white-space:pre"> </span>Sprite* pSprite = Sprite::create("loading.png"); <span style="white-space:pre"> </span>pSprite->runAction(RepeatForever::create(RotateBy::create(1.5f, 360.0f))); <span style="white-space:pre"> </span>pSprite->setPosition(kWindowWidth / 2, kWindowHeight / 2); <span style="white-space:pre"> </span>addChild(pSprite); <span style="white-space:pre"> </span>m_pLoadingLabel = Label::createWithSystemFont("0", "宋体", 30); <span style="white-space:pre"> </span>m_pLoadingLabel->setColor(Color3B(255, 255, 255)); <span style="white-space:pre"> </span>m_pLoadingLabel->setPosition(pSprite->getPosition()); <span style="white-space:pre"> </span>addChild(m_pLoadingLabel); <span style="white-space:pre"> </span>Director::getInstance()->getTextureCache()->addImageAsync( <span style="white-space:pre"> </span>m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1)); <span style="white-space:pre"> </span>++m_iLoadingNum; <span style="white-space:pre"> </span>scheduleUpdate(); } void LoadingLayer::update(float dt) { <span style="white-space:pre"> </span&g 9584 t;m_fPassTime += dt; <span style="white-space:pre"> </span>if (m_fPassTime > 0.5f && m_bImageHasLoading == false) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>m_bImageHasLoading = true; <span style="white-space:pre"> </span>Director::getInstance()->getTextureCache()->addImageAsync( <span style="white-space:pre"> </span>m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1)); <span style="white-space:pre"> </span>++m_iLoadingNum; <span style="white-space:pre"> </span>} } void LoadingLayer::AddImageCallBack(Texture2D *texture) { <span style="white-space:pre"> </span>if (m_iLoadingNum == 1) <span style="white-space:pre"> </span>m_bImageHasLoading = true; <span style="white-space:pre"> </span>m_pLoadingLabel->setString( <span style="white-space:pre"> </span>String::createWithFormat("%d", 100 * m_iLoadingNum / (m_stvImageNames.size() + m_stvAniNames.size()))->getCString() ); <span style="white-space:pre"> </span>if (m_iLoadingNum < m_stvImageNames.size()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>Director::getInstance()->getTextureCache()->addImageAsync( <span style="white-space:pre"> </span>m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1)); <span style="white-space:pre"> </span>++m_iLoadingNum; <span style="white-space:pre"> </span>}else{ <span style="white-space:pre"> </span>AddAniCallBack(0.0f); <span style="white-space:pre"> </span>} } void LoadingLayer::AddAniCallBack(float dt) { <span style="white-space:pre"> </span>m_pLoadingLabel->setString( <span style="white-space:pre"> </span>String::createWithFormat("%d", 100 * m_iLoadingNum / (m_stvImageNames.size() + m_stvAniNames.size()))->getCString() ); <span style="white-space:pre"> </span>if (m_iLoadingNum == m_stvImageNames.size() + m_stvAniNames.size()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>m_pLoadingLabel->setString("100"); <span style="white-space:pre"> </span>GameLayer* pGameLayer = GameLayer::create(); <span style="white-space:pre"> </span>getParent()->addChild(pGameLayer); <span style="white-space:pre"> </span>removeFromParent(); <span style="white-space:pre"> </span>}else{ <span style="white-space:pre"> </span>cocostudio::ArmatureDataManager::getInstance()->addArmatureFileInfoAsync( <span style="white-space:pre"> </span>m_stvAniNames[m_iLoadingNum -m_stvImageNames.size()], this,<span style="white-space:pre"> </span>schedule_selector(LoadingLayer::AddAniCallBack)); <span style="white-space:pre"> </span>++m_iLoadingNum; <span style="white-space:pre"> </span>} }
相关文章推荐
- Cocos2d-x中背景音乐和音效使用实例
- Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验
- Cocos2d-x UI开发之CCControlPotentiometer控件类使用实例
- Cocos2d-x UI开发之文本类使用实例
- Cocos2d-x保存用户游戏数据之XML文件是否存在问题判断方法
- Cocos2d-x UI开发之菜单类使用实例
- Cocos2d-x UI开发之CCControlButton控件类实例
- Cocos2d-x学习笔记之世界坐标系、本地坐标系、opengl坐标系、屏幕坐标系
- 异步加载技术实现当滚动条到最底部的瀑布流效果
- javascript动态加载二
- Blocksit插件实现瀑布流数据无限( 异步)加载
- JavaScript 动态加载脚本和样式的方法
- jquery预加载图片的方法
- 动态加载jQuery的两种方法实例分析
- 一个简单的动态加载js和css的jquery代码
- jquery select动态加载选择(兼容各种浏览器)
- 如何调试异步加载页面里包含的js文件
- 使用jQuery异步加载 JavaScript脚本解决方案
- 使用jquery动态加载javascript以减少服务器压力
- js异步加载的三种解决方案