Cocos2dx之进度条使用,异步加载资源进缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangxuan0261/article/details/21257919

在游戏前通常都会加载好游戏中需要用到的资源,游戏的时候绘图才不会让内存飙升,不那么卡顿,

加载资源存到缓存时有一点点需要等待的时间,那么就设置一个场景显示一下进度条,加载完成就切换场景进入主游戏

进度条所在的头文件

class LoadingScene : public cocos2d::CCLayer
{
public:
	CREATE_FUNC(LoadingScene);
	bool init();
	static cocos2d::CCScene* scene();
	LoadingScene();
	~LoadingScene();
	void resources();	//需要加载资源都放到这个函数里了
	void loadCallBack(cocos2d::CCObject* object);//异步加载完成后回调主程序的函数
private:
	int count;//加载计数
	int total;//总资源个数
	cocos2d::CCLabelTTF* label;//显示加载进度文字
};


以下是实现

bool LoadingScene::init()
{
	count = 0;
	total = 51;

	CCSize screen = CCDirector::sharedDirector()->getVisibleSize();
	float width = screen.width;
	float height = screen.height;

	//进度条背景
	CCSprite* loadBg = CCSprite::create("gmbg/lodingbg.png");
	loadBg->setPosition(ccp(width / 2, height / 2));
	this->addChild(loadBg);

	//进度条
	CCSprite* loading = CCSprite::create("gmbg/longding.png");
	CCProgressTimer* pt = CCProgressTimer::create(loading);

	pt->setMidpoint(ccp(0, 0));
	pt->setType(kCCProgressTimerTypeBar);
	pt->setBarChangeRate(ccp(1, 0));
	pt->setPercentage(0);
	pt->setPosition(ccp(width / 2, height / 2 - 5));
	this->addChild(pt, 1, 3);

	label = CCLabelTTF::create("Loading ……", "arial", 30);
	label->setColor(ccc3(255, 255, 0));
	label->setPosition(ccp(width / 2, height / 2 + 100));
	this->addChild(label, 2, 5);

	//圆形进度显示
	CCSprite* radianBg = CCSprite::create("game/jnl.png");
	radianBg->setPosition(ccp(width / 2, height / 2 - 100));
	this->addChild(radianBg);
	
	CCSprite* radianing = CCSprite::create("game/jnzt.png");
	CCProgressTimer* ptRadian = CCProgressTimer::create(radianing);
	ptRadian->setPosition(ccp(width / 2, height / 2 - 100));
	ptRadian->setPercentage(50);
	ptRadian->setType(kCCProgressTimerTypeRadial);
	this->addChild(ptRadian, 1, 98);


	this->resources();
	return true;
}
void LoadingScene::resources()
{

	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/welcomebg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 背景图片
	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/coder.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 添加关于开发者背景图片

	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/button_sound_on.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 声音开始
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/button_sound_off.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 声音关闭

	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/coder_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 开发者按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/coder_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 开发者按钮

	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/return_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 返回菜单按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/return_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 返回菜单按钮


	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/star_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 开始菜单按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/star_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面  


	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/gamebg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 背景图片

	CCTextureCache::sharedTextureCache()->addImageAsync("game/weapon.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 弓弩武器
	CCTextureCache::sharedTextureCache()->addImageAsync("game/wq0.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  弓箭子弹


	CCTextureCache::sharedTextureCache()->addImageAsync("game/monster_blood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 血条背景
	CCTextureCache::sharedTextureCache()->addImageAsync("game/monster_blood_frame.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  血条


	CCTextureCache::sharedTextureCache()->addImageAsync("monster/dutu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 赌徒怪物

	CCTextureCache::sharedTextureCache()->addImageAsync("game/zcblood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 主城血条
	CCTextureCache::sharedTextureCache()->addImageAsync("game/magic.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  主城魔法条
	CCTextureCache::sharedTextureCache()->addImageAsync("game/panelblood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  主城血条和魔法条的背景



	CCTextureCache::sharedTextureCache()->addImageAsync("game/jnl.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 魔法阵背景图片
	CCTextureCache::sharedTextureCache()->addImageAsync("game/jnzt.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 魔法阵CD 亮的图片


	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/pause_button.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏暂停按钮

	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_home_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏家按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_home_up.png", this, callfuncO_selector(LoadingScene::loadCallBack));
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_resume_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏继续按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_resume_up.png", this, callfuncO_selector(LoadingScene::loadCallBack));
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_retry_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏重新开始按钮
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_rety_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏暂停按钮


	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/pause_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图是游戏暂停按钮


	CCTextureCache::sharedTextureCache()->addImageAsync("game/MagicMatrix.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图魔法阵的图片

	CCTextureCache::sharedTextureCache()->addImageAsync("specia/ligtht.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图闪电特效图片
	CCTextureCache::sharedTextureCache()->addImageAsync("specia/diyu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图地狱石块特效图片
	CCTextureCache::sharedTextureCache()->addImageAsync("specia/long.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图二龙戏珠特效图片
	CCTextureCache::sharedTextureCache()->addImageAsync("specia/thumbnails.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图特效缩略图片



	CCTextureCache::sharedTextureCache()->addImageAsync("game/woniubj.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图地蜗牛进度条背景图片
	CCTextureCache::sharedTextureCache()->addImageAsync("game/woniujd.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此图蜗牛进度条图片
	CCTextureCache::sharedTextureCache()->addImageAsync("game/woniu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图蜗牛图片

	CCTextureCache::sharedTextureCache()->addImageAsync("game/stage_title.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图等级的背景图


	CCTextureCache::sharedTextureCache()->addImageAsync("game/gameover.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图游戏结束标题图
	CCTextureCache::sharedTextureCache()->addImageAsync("game/gameovertips.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图游戏结束提示图
	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/gameoverbg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图游戏结背景图

	CCTextureCache::sharedTextureCache()->addImageAsync("game/coin.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  此图金币图标

	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/stats_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  胜利界面背景图片

	CCTextureCache::sharedTextureCache()->addImageAsync("game/statstip.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  胜利界面提示按钮

	CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/research_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  能力提升界面的背景图片
	CCTextureCache::sharedTextureCache()->addImageAsync("game/lvSyspng.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  能力提升界面的按钮图片
	CCTextureCache::sharedTextureCache()->addImageAsync("game/lvinfo.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  能力提升界面的说明文字图片



	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/outdown.png", this, callfuncO_selector(LoadingScene::loadCallBack)); //  游戏结束按钮图片
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/outup.png", this, callfuncO_selector(LoadingScene::loadCallBack));
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/updwon.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 升级系统图片
	CCTextureCache::sharedTextureCache()->addImageAsync("gmme/upup.png", this, callfuncO_selector(LoadingScene::loadCallBack));

}

加载资源后进度的计算

void LoadingScene::loadCallBack(CCObject* object)
{
	count++;
	CCProgressTimer* pt = (CCProgressTimer*)this->getChildByTag(3);
	int percentage = (int)count * 100 / total;
	char tem[32];
	sprintf(tem, "Loading %d", percentage);
	CCLOG(tem);
	label->setString(tem);
	pt->setPercentage(percentage);

	CCProgressTimer* ptRadian = (CCProgressTimer*)this->getChildByTag(98);
	ptRadian->setPercentage(percentage);

	if (count == total)
	{
		//加载完成就切换场景
		CCScene* hello = HelloWorld::scene();
		CCTransitionMoveInL* effect = CCTransitionMoveInL::create(2.0f, hello);
		CCDirector::sharedDirector()->replaceScene(effect);
	}
}

效果:

加载过程



加载完成切换场景



阅读更多
想对作者说点什么?
相关热词

博主推荐

换一批

没有更多推荐了,返回首页