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

一个异步加载图片资源和动画资源的ladoing界面。下面给代码

.h文件

#ifndef RUN_SCENE_LOADINGLAYER_H__
#define RUN_SCENE_LOADINGLAYER_H__


#include "cocos2d.h"


using namespace cocos2d;


class LoadingLayer : public Layer
{
public:
	LoadingLayer();
	~LoadingLayer();


	virtual bool init();
	CREATE_FUNC(LoadingLayer);
	virtual void onEnter();
	virtual void update(float dt);


	void AddImageCallBack(Texture2D *texture);	//加图片
	void AddAniCallBack(float dt);				//加动画
protected:
	std::vector m_stvImageNames;		//所有要加载的图片名称
	std::vector m_stvAniNames;			//所有要加载的动画名称
	std::vector m_stvSpriteFrameName;	//所有要加载的大图名称
	ProgressTimer* m_pLoadingProress;				//进度条
	Label* m_pLoadingLabel;


	int m_iLoadingNum;
	float m_fPassTime;
	bool m_bImageHasLoading;
};


#endif // LOADINGLAYER_H__



.cpp

#include "LoadingLayer.h"
#include "GameScene.h"
#include "data\game_constant_defines.h"


LoadingLayer::LoadingLayer() : 
	m_pLoadingProress(nullptr),
	m_fPassTime(0.0f),
	m_bImageHasLoading(false)
{
	m_pLoadingLabel = nullptr;
	m_iLoadingNum = 0;
	m_stvImageNames.clear();
	m_stvAniNames.clear();
	m_stvSpriteFrameName.clear();
}


LoadingLayer::~LoadingLayer()
{


}


bool LoadingLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}


	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",
		"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", 
		"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" };
	for (int i = 0; i != sizeof(imageNames) / sizeof(imageNames[0]); ++i)
	{
		m_stvImageNames.push_back(imageNames[i]);
	}


	const char* aniNames[] = { "ani/ll_bea/ll_bea.ExportJson", "ani/ll_missile/ll_missile.ExportJson", "ani/ll/ll.ExportJson", "ani/chui/chui.ExportJson",
		"ani/public/shanxian.ExportJson", "ani/fa/fa.ExportJson", "ani/fa_missile/fa_missile.ExportJson", "ani/buff/buff.ExportJson", "ani/pao/pao.ExportJson",
		"ani/pao_missile/pao_missile.ExportJson", "ani/shu1/shu1.ExportJson" };
	for (int i = 0; i != sizeof(aniNames) / sizeof(aniNames[0]); ++i)
	{
		m_stvAniNames.push_back(aniNames[i]);
	}
	//const char* frameNames[] = { "ani/ll/ll0", "ani/chui/chui0" };
	//for (int i = 0; i != sizeof(frameNames) / sizeof(frameNames[0]); ++i)
	//{
	//	m_stvSpriteFrameName.push_back(frameNames[i]);
	//}
	先加入plist
	//for (int i = 0; i != m_stvSpriteFrameName.size(); ++i)
	//{
	//	std::string tempString = m_stvSpriteFrameName[i];
	//	SpriteFrameCache::getInstance()->addSpriteFramesWithFile(m_stvSpriteFrameName[i].append(".plist"));
	//	m_stvSpriteFrameName[i] = tempString;
	//}
	再把png放入待加载容器
	//for (int i = 0; i != m_stvSpriteFrameName.size(); ++i)
	//	m_stvImageNames.push_back(m_stvSpriteFrameName[i].append(".png"));




	return true;
}


void LoadingLayer::onEnter()
{
	Layer::onEnter();


	Sprite* pSprite = Sprite::create("loading.png");
	pSprite->runAction(RepeatForever::create(RotateBy::create(1.5f, 360.0f)));
	pSprite->setPosition(kWindowWidth / 2, kWindowHeight / 2);
	addChild(pSprite);


	m_pLoadingLabel = Label::createWithSystemFont("0", "宋体", 30);
	m_pLoadingLabel->setColor(Color3B(255, 255, 255));
	m_pLoadingLabel->setPosition(pSprite->getPosition());
	addChild(m_pLoadingLabel);


	Director::getInstance()->getTextureCache()->addImageAsync(
		m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1));
	++m_iLoadingNum;


	scheduleUpdate();
}


void LoadingLayer::update(float dt)
{
	m_fPassTime += dt;


	if (m_fPassTime > 0.5f && m_bImageHasLoading == false)
	{
		m_bImageHasLoading = true;
		Director::getInstance()->getTextureCache()->addImageAsync(
			m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1));
		++m_iLoadingNum;
	}
}


void LoadingLayer::AddImageCallBack(Texture2D *texture)
{
	if (m_iLoadingNum == 1)
		m_bImageHasLoading = true;


	m_pLoadingLabel->setString(
		String::createWithFormat("%d", 100 * m_iLoadingNum / (m_stvImageNames.size() + m_stvAniNames.size()))->getCString() );
	if (m_iLoadingNum < m_stvImageNames.size())
	{
		Director::getInstance()->getTextureCache()->addImageAsync(
			m_stvImageNames[m_iLoadingNum], std::bind(&LoadingLayer::AddImageCallBack, this, std::placeholders::_1));
		++m_iLoadingNum;
	}else{
		AddAniCallBack(0.0f);
	}
}


void LoadingLayer::AddAniCallBack(float dt)
{
	m_pLoadingLabel->setString(
		String::createWithFormat("%d", 100 * m_iLoadingNum / (m_stvImageNames.size() + m_stvAniNames.size()))->getCString() );


	if (m_iLoadingNum == m_stvImageNames.size() + m_stvAniNames.size())
	{
		m_pLoadingLabel->setString("100");
		GameLayer* pGameLayer = GameLayer::create();
		getParent()->addChild(pGameLayer);
		removeFromParent();
	}else{
		cocostudio::ArmatureDataManager::getInstance()->addArmatureFileInfoAsync(
			m_stvAniNames[m_iLoadingNum -m_stvImageNames.size()], this,	schedule_selector(LoadingLayer::AddAniCallBack));


		++m_iLoadingNum;
	}
}





你可能感兴趣的:(Cocos2d-x)