剛開始做卡牌的時候沒有想到要做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",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));
//-------------------------------------------------------------卡牌
//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, CC_CALLBACK_1(Loading::load_pic, this));
}
//equip
for(int i=0;i<=CARD_EQUIP_NUM;i++)
{
sprintf(file_name_c,"card/equip/card_equip_%d.jpg",i);
std::string file_name(file_name_c);
CCTextureCache::sharedTextureCache()->addImageAsync(file_name, CC_CALLBACK_1(Loading::load_pic, this));
}
//hero
for(int i=0;i<=CARD_HERO_NUM;i++)
{
sprintf(file_name_c,"card/people/card_hero_%d.jpg",i);
std::string file_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++;
}
注意加載圖片和精靈表的回調函數是不一樣的,因爲加載這兩種資源需要不同的方式
以下爲一個自定義函數和兩個回調函數(用於動態顯示加載進度和加載):
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");