At the beginning of the card did not expect to do loading, because the small game resources are not many.
But then continue to add pictures, until the real machine to find the problem of stalling, I know that need to add loading ...
First, we define the class first:
Class Loading:public Layer{public:bool init (); Create_func (Loading); static scene* createscene (); int total_pic_num;//load picture int total_sound_num;//need to load number of sounds int all_num ///Total number of loads required int load_num;//loaded quantity std::vector<std::string> plist_name;//plist name int load_plist_num;// Several plistvoid load_sound (int num) were loaded, void Load_pic (object* psender), void Load_plist (object* psender);};
Initialize some of the parameters in the CPP first:
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 name Plist_name.push_back ("Ui/button_action/button_action");p list_name.push_back ("UI/touch_action/ Touch_act_three ");p list_name.push_back (" Skill/atk/flame ");p list_name.push_back (" Skill/atk/freeze ");p List_ Name.push_back ("skill/atk/lightning");p list_name.push_back ("Skill/atk/wind");
Then start loading the resources:
-------------------------------------------------------------pre-loaded background music cocosdenshion::simpleaudioengine:: GetInstance ()->preloadbackgroundmusic ("Sound/bgm/bgm_start.mp3"); Cocosdenshion::simpleaudioengine::getinstance ()->preloadbackgroundmusic ("Sound/bgm/bgm_game.mp3"); Load_ Sound (bgm_num);//-------------------------------------------------------------preloaded audio cocosdenshion:: Simpleaudioengine::getinstance ()->preloadeffect ("Sound/effect/button_1.mp3");//Attack sound 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");// Control Sound 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);//-------------------------------------------------------------background Cctexturecache:: Sharedtexturecache ()->addimageasync ("Ui/background/background_start.jpg", Cc_callback_1 (Loading::load_pic, This)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/background_help.jpg", CC_CALLBACK_1 ( Loading::load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/background_about.jpg", CC_CALLBACK_1 ( Loading::load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/help_word.jpg", Cc_callback_1 (Loading::load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/background_game.jpg", CC_CALLBACK_1 ( Loading::load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/setting_back.png", CC_CALLBACK_1 (Loading: : Load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/setting_face.png", CC_CALLBACK_1 (Loading: : Load_pic, this)); Cctexturecache::sharedtexturecache ()->addimageasync ("Ui/background/title.png", CC_CALLBACK_1 (Loading::load_ Pic, this));//-------------------------------------------------------------card//backchar 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::stringfile_name ( File_name_c); Cctexturecache::sharedtexturecache ()->addimageasync (file_name, Cc_callback_1 (Loading::load_pic, this));} equipfor (int i=0;i<=card_equip_num;i++) {sprintf (File_name_c, "card/equip/card_equip_%d.jpg", i); std:: Stringfile_name (FILe_name_c); Cctexturecache::sharedtexturecache ()->addimageasync (file_name, Cc_callback_1 (Loading::load_pic, this));} herofor (int i=0;i<=card_hero_num;i++) {sprintf (File_name_c, "card/people/card_hero_%d.jpg", i); std:: Stringfile_name (File_name_c); Cctexturecache::sharedtexturecache ()->addimageasync (file_name, Cc_callback_1 (Loading::load_pic, this));} -------------------------------------------------------------Load Wizard table for (std::string a:plist_name) { Cctexturecache::sharedtexturecache ()->addimageasync (A + ". png", Cc_callback_1 (Loading::load_plist, this));// load_plist_num++;}
Note that the callback function that loads the picture and the sprite table is not the same, because loading the two resources requires a different way
Here is a custom function and two callback functions (for dynamic display of loading progress and loading):
void loading::load_sound (int num)//This function can be ignored without looking at {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)//The callback function that loads the picture {Load_num+=1;int percent= ((float) load_num/(float) all_num) *100; This->runaction (Delaytime::create (15));//Rest Auto Loading_font = (label*) getchildbytag (1);loading_font-> SetString (String::createwithformat ("loading......%d%%", percent)->_string); if (percent>=100) {Director:: GetInstance ()->replacescene (Start::createscene ());//If the load is completed jump}}void loading::load_plist (object* pSender)// Load the wizard table's callback function {spriteframecache::getinstance ()->addspriteframeswithfile (plist_name.at (load_plist_num) + ". plist", plist_name.at (Load_plist_num) + ". png");//The picture has been loaded and appears to be read directly from the cache in order to load the picture with the plist load_plist_num++;this->runaction (delaytime::create); Load_num+=1;int percent= ((float) load_num/(float) all_num) *100;this->ruNaction (delaytime::create), Auto Loading_font = (label*) getchildbytag (1); Loading_font->setstring (String:: Createwithformat ("loading......%d%%", percent)->_string); if (percent>=100) {director::getinstance () Replacescene (Start::createscene ());}}
As above is the asynchronous load method, now we need to know how to get from the cache:
Get Pictures:
Sprite *bk=sprite::createwithtexture (Texturecache::sharedtexturecache ()->textureforkey ("UI/background/ Background_start.jpg "));
Get the wizard table:
Ccspriteframecache *framecache=ccspriteframecache::sharedspriteframecache (); framecache->getspriteframebyname ("Ui/touch_action/touch_act_three.plist");
So that we can get the resources from the cache, and I tested it really much faster.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cocos2d-x 3.3 Card Design No.6 loading interface (loading pictures asynchronously, plist)