您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x 3.x 异步加载资源和Loading界面

2015-09-22 15:41 393 查看
一个异步加载图片资源和动画资源的ladoing界面。下面给代码

.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>}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息