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

刚开始做卡牌的时候没有想到要做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;//加载了几个plistvoid 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",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));//————————————————————-卡牌//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));}//————————————————————-加载精灵表for(std::string a : plist_name){CCTextureCache::sharedTextureCache()->addImageAsync(a+".png", CC_CALLBACK_1(Loading::load_plist, this));//load_plist_num++;}注意加载图片和精灵表的回调函数是不一样的,,因为加载这两种资源需要不同的方式我没有值得分享的感伤爱情故事,

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

相关文章:

你感兴趣的文章:

标签云: