cocos2d-x 3.3 之卡牌设计 NO.6 Loading界面(异步加载图片,plist)

cocos2d-x 3.3 之卡牌设计 NO.6 Loading界面(异步加载图片,plist),第1张

概述刚开始做卡牌的时候没有想到要做loading,因为小游戏资源不多。 但是后来不断的加图片,直到在真机上发现卡顿的问题,我才知道该需要加loading了...... 首先,我们先定义类: class Loading : public Layer{public: bool init(); CREATE_FUNC( Loading); static Scene* CreateScene();

刚开始做卡牌的时候没有想到要做loading,因为小游戏资源不多。

但是后来不断的加图片,直到在真机上发现卡顿的问题,我才知道该需要加loading了......


首先,我们先定义类:

class Loading : public Layer{public:	bool init();	CREATE_FUNC( Loading);	static Scene* CreateScene();	int total_pic_num;//需加载图片数	int total_sound_num;//需加载声音数	int all_num;//总共需要加载的数量	int load_num;//已加载数量	std::vector<std::string> pList_name;//pList名	int load_pList_num;//加载了几个pList	voID load_sound(int num);	voID load_pic(Object* pSender);	voID load_pList(Object* pSender);};

在cpp中先初始化一些参数:
total_pic_num=BACKGROUND_NUM+CARD_HERO_NUM+CARD_EQUIP_NUM+CARD_BACK_NUM+button_ACTION_NUM+touch_ACTION_NUM+CARD_SKILL_NUM;total_sound_num=BGM_NUM+EFFECT_NUM;all_num=total_pic_num+total_sound_num;load_num=0;load_pList_num=0;//pList名pList_name.push_back("UI/button_action/button_action");pList_name.push_back("UI/touch_action/touch_act_three");pList_name.push_back("skill/atk/flame");pList_name.push_back("skill/atk/freeze");pList_name.push_back("skill/atk/lightning");pList_name.push_back("skill/atk/wind");

然后开始加载资源吧:
//-------------------------------------------------------------预加载背景音乐	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/bgm/bgm_start.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/bgm/bgm_game.mp3");	load_sound(BGM_NUM);	//-------------------------------------------------------------预加载音效	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/button_1.mp3");	//攻击音效	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/button_out.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/flame_shoot.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/freeze_shoot.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/lightning_shoot.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/wind_shoot.mp3");	//控制音效	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/open_card.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/card_out.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/equip_out.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/no.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/effect/card_dIE.mp3");	load_sound(EFFECT_NUM);//-------------------------------------------------------------背景	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/background_start.jpg",CC_CALLBACK_1(Loading::load_pic,this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/background_help.jpg",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/background_about.jpg",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/help_word.jpg",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/background_game.jpg",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/setting_back.png",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/setting_face.png",this));	CCTextureCache::sharedTextureCache()->addImageAsync("UI/background/Title.png",this));//-------------------------------------------------------------卡牌	//back	char file_name_c[100];	for(int i=0;i<=CARD_BACK_NUM;i++)	{		sprintf(file_name_c,"card/back/card_need_magic_%d.jpg",i);		std::string	file_name(file_name_c);		CCTextureCache::sharedTextureCache()->addImageAsync(file_name,this));	}	//equip	for(int i=0;i<=CARD_EQUIP_NUM;i++)	{		sprintf(file_name_c,"card/equip/card_equip_%d.jpg",this));	}		//hero	for(int i=0;i<=CARD_HERO_NUM;i++)	{		sprintf(file_name_c,"card/people/card_hero_%d.jpg",this));	}//-------------------------------------------------------------加载精灵表	for(std::string a : pList_name)	{		CCTextureCache::sharedTextureCache()->addImageAsync(a+".png",CC_CALLBACK_1(Loading::load_pList,this));		//load_pList_num++;	}
注意加载图片和精灵表的回调函数是不一样的,因为加载这两种资源需要不同的方式

以下为一个自定义函数和两个回调函数(用于动态显示加载进度和加载):

voID Loading::load_sound(int num)//这个函数可以忽略不看{	load_num+=num;	int percent=((float)load_num/(float)all_num)*100;	auto loading_Font = (Label*)getChildByTag(1);	loading_Font->setString(String::createWithFormat("Loading......%d%%",percent)->_string);}voID Loading::load_pic(Object* pSender)//加载图片的回调函数{	load_num+=1;	int percent=((float)load_num/(float)all_num)*100;	this->runAction(DelayTime::create(15));//休息	auto loading_Font = (Label*)getChildByTag(1);	loading_Font->setString(String::createWithFormat("Loading......%d%%",percent)->_string);	if(percent>=100)	{		Director::getInstance()->replaceScene(Start::CreateScene());//如果加载完成跳转	}}voID Loading::load_pList(Object* pSender)//加载精灵表的回调函数{	SpriteFrameCache::getInstance()->addSpriteFramesWithfile(pList_name.at(load_pList_num)+".pList",pList_name.at(load_pList_num)+".png");//图片已经加载过貌似会直接从缓存中读取,目的是把图片和pList一起加载	load_pList_num++;	this->runAction(DelayTime::create(15));	load_num+=1;	int percent=((float)load_num/(float)all_num)*100;	this->runAction(DelayTime::create(15));	auto loading_Font = (Label*)getChildByTag(1);	loading_Font->setString(String::createWithFormat("Loading......%d%%",percent)->_string);	if(percent>=100)	{		Director::getInstance()->replaceScene(Start::CreateScene());	}	}


如上就是异步加载的方法,现在我们需要知道如何从缓存中获取:

获取图片:

Sprite *BK=Sprite::createWithTexture(TextureCache::sharedTextureCache()->textureForKey("UI/background/background_start.jpg"));  

获取精灵表:
CCSpriteFrameCache *frameCache=CCSpriteFrameCache::sharedSpriteFrameCache();frameCache->getSpriteFrameByname("UI/touch_action/touch_act_three.pList");


这样就能从缓存中获取资源了,我测试了下真的快很多 总结

以上是内存溢出为你收集整理的cocos2d-x 3.3 之卡牌设计 NO.6 Loading界面(异步加载图片,plist)全部内容,希望文章能够帮你解决cocos2d-x 3.3 之卡牌设计 NO.6 Loading界面(异步加载图片,plist)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1063074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存