Cocos2d-x 3.3 card design No. 6 Loading interface (asynchronous Loading pictures, plist)
I didn't think of loading when I first started playing cards, because there were not many game resources.
However, I continued to add images until I found a choppy problem on the real machine. I realized that I needed to add loading ......
First, we first define the class:
Class Loading: public Layer {public: bool init (); CREATE_FUNC (Loading); static Scene * CreateScene (); int total_pic_num; // int total_sound_num; // The number of sounds to be loaded int all_num; // The total number of sounds to be loaded int load_num; // The number of loaded std: vector
Plist_name; // plist name int load_plist_num; // loads several plistvoid load_sound (int num); void load_pic (Object * pSender); void load_plist (Object * pSender );};
Initialize some parameters in cpp:
Worker = BACKGROUND_NUM + worker + CARD_BACK_NUM + BUTTON_ACTION_NUM + TOUCH_ACTION_NUM + CARD_SKILL_NUM; worker = BGM_NUM + worker t_num; all_num = worker + worker; load_num = 0; worker = 0; // plist name (UI/button_action); UI/touch_action/touch_act_three); plist_name.push_back (skill/atk/flame); trim (skill/atk/freeze ); plist_name.push_back (skill/atk/lightning); plist_name.push_back (skill/atk/wind );
Then, load the resource:
// Encode pre-loaded background music CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic (sound/bgm/bgm_startaudio); CocosDenshion: SimpleAudioEngine: getInstance () -> preloadBackgroundMusic (sound/bgm/bgm_gameaudio); load_sound (BGM_NUM); // prepare the preloaded sound CocosDenshion: SimpleAudioEngine: getInstance () -> preloadEffect (sound/effect/button_1.mp3); // CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect (sound/effect/button_outeffects); usage: SimpleAudioEngine:: getInstance ()-> preloadEffect (sound/effect/flame_shootect); usage: SimpleAudioEngine: getInstance ()-> preloadEffect (sound/effect/freeze_shootect); usage: SimpleAudioEngine:: getInstance ()-> preloadEffect (sound/effect/lightning_shootion); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect (sound/effect/wind_shootect ); // CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect (sound/effect/open_card.mp3); CocosDenshion: SimpleAudioEngine: getInstance () -> preloadEffect (sound/effect/card_outeffects); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect (sound/effect/equip_outeffects); usage: 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, 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/callback, loads (Loading: load_pic, this); CCTextureCache: loads ()-> 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/callback, CC_CALLBACK_1 (Loading: load_pic, this); CCTextureCache: sharedTextureCache ()-> addImageAsync (UI/background/title.png, CC_CALLBACK_1 (Loading: load_pic, this )); // notify card // backchar file_name_c [100]; for (int I = 0; I <= CARD_BACK_NUM; I ++) {sprintf (file_name_c, card/back/card_need_magic_1_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_0000d.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/users, I); std: stringfile_name (file_name_c); CCTextureCache: sharedTextureCache ()-> addImageAsync (file_name, CC_CALLBACK_1 (Loading: load_pic, this);} // loads the genie 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 for loading images and sprite tables is different, because loading these two resources requires different methods.
The following is a custom function and two callback functions (used to dynamically display the loading progress and loading ):
Void Loading: load_sound (int num) // This function can ignore not to see {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) // callback function for Loading images {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 Loading is completed, jump} void Loading: load_plist (Object * pSender) // The callback function for Loading the genie table {SpriteFrameCache: getInstance ()-> callback ); // if the image has been loaded, it seems that it will be directly read from the cache. The purpose is to load the image with 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 ());}}
The above is the asynchronous loading method. Now we need to know how to get it from the cache:
Get image:
Sprite *BK=Sprite::createWithTexture(TextureCache::sharedTextureCache()->textureForKey(UI/background/background_start.jpg));
Get the genie table:
CCSpriteFrameCache *frameCache=CCSpriteFrameCache::sharedSpriteFrameCache();frameCache->getSpriteFrameByName(UI/touch_action/touch_act_three.plist);
In this way, you can get resources from the cache. I tested it a lot faster.