Cocos2dx之進度條使用,非同步載入資源進快取
在遊戲前通常都會載入好遊戲中需要用到的資源,遊戲的時候繪圖才不會讓記憶體飆升,不那麼卡頓,
載入資源存到快取時有一點點需要等待的時間,那麼就設定一個場景顯示一下進度條,載入完成就切換場景進入主遊戲
進度條所在的標頭檔案
class LoadingScene : public cocos2d::CCLayer
{
public:
CREATE_FUNC(LoadingScene);
bool init();
static cocos2d::CCScene* scene();
LoadingScene();
~LoadingScene();
void resources(); //需要載入資源都放到這個函式裡了
void loadCallBack(cocos2d::CCObject* object);//非同步載入完成後回撥主程式的函式
private:
int count;//載入計數
int total;//總資源個數
cocos2d::CCLabelTTF* label;//顯示載入進度文字
};
以下是實現
bool LoadingScene::init()
{
count = 0;
total = 51;
CCSize screen = CCDirector::sharedDirector()->getVisibleSize();
float width = screen.width;
float height = screen.height;
//進度條背景
CCSprite* loadBg = CCSprite::create("gmbg/lodingbg.png");
loadBg->setPosition(ccp(width / 2, height / 2));
this->addChild(loadBg);
//進度條
CCSprite* loading = CCSprite::create("gmbg/longding.png");
CCProgressTimer* pt = CCProgressTimer::create(loading);
pt->setMidpoint(ccp(0, 0));
pt->setType(kCCProgressTimerTypeBar);
pt->setBarChangeRate(ccp(1, 0));
pt->setPercentage(0);
pt->setPosition(ccp(width / 2, height / 2 - 5));
this->addChild(pt, 1, 3);
label = CCLabelTTF::create("Loading ……", "arial", 30);
label->setColor(ccc3(255, 255, 0));
label->setPosition(ccp(width / 2, height / 2 + 100));
this->addChild(label, 2, 5);
//圓形進度顯示
CCSprite* radianBg = CCSprite::create("game/jnl.png");
radianBg->setPosition(ccp(width / 2, height / 2 - 100));
this->addChild(radianBg);
CCSprite* radianing = CCSprite::create("game/jnzt.png");
CCProgressTimer* ptRadian = CCProgressTimer::create(radianing);
ptRadian->setPosition(ccp(width / 2, height / 2 - 100));
ptRadian->setPercentage(50);
ptRadian->setType(kCCProgressTimerTypeRadial);
this->addChild(ptRadian, 1, 98);
this->resources();
return true;
}
void LoadingScene::resources()
{
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/welcomebg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/coder.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 新增關於開發者背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/button_sound_on.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 聲音開始
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/button_sound_off.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 聲音關閉
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/coder_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 開發者按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/coder_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 開發者按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/return_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 返回選單按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/return_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 返回選單按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/star_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 開始選單按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/star_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/gamebg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 歡迎介面 背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/weapon.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 弓弩武器
CCTextureCache::sharedTextureCache()->addImageAsync("game/wq0.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 弓箭子彈
CCTextureCache::sharedTextureCache()->addImageAsync("game/monster_blood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 血條背景
CCTextureCache::sharedTextureCache()->addImageAsync("game/monster_blood_frame.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 血條
CCTextureCache::sharedTextureCache()->addImageAsync("monster/dutu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 賭徒怪物
CCTextureCache::sharedTextureCache()->addImageAsync("game/zcblood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 主城血條
CCTextureCache::sharedTextureCache()->addImageAsync("game/magic.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 主城魔法條
CCTextureCache::sharedTextureCache()->addImageAsync("game/panelblood.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 主城血條和魔法條的背景
CCTextureCache::sharedTextureCache()->addImageAsync("game/jnl.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 魔法陣背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/jnzt.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 魔法陣CD 亮的圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/pause_button.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲暫停按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_home_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲家按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_home_up.png", this, callfuncO_selector(LoadingScene::loadCallBack));
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_resume_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲繼續按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_resume_up.png", this, callfuncO_selector(LoadingScene::loadCallBack));
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_retry_down.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲重新開始按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/btn_rety_up.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲暫停按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/pause_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖是遊戲暫停按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("game/MagicMatrix.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖魔法陣的圖片
CCTextureCache::sharedTextureCache()->addImageAsync("specia/ligtht.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖閃電特效圖片
CCTextureCache::sharedTextureCache()->addImageAsync("specia/diyu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖地獄石塊特效圖片
CCTextureCache::sharedTextureCache()->addImageAsync("specia/long.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖二龍戲珠特效圖片
CCTextureCache::sharedTextureCache()->addImageAsync("specia/thumbnails.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖特效縮圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/woniubj.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖地蝸牛進度條背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/woniujd.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖蝸牛進度條圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/woniu.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖蝸牛圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/stage_title.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖等級的背景圖
CCTextureCache::sharedTextureCache()->addImageAsync("game/gameover.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖遊戲結束標題圖
CCTextureCache::sharedTextureCache()->addImageAsync("game/gameovertips.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖遊戲結束提示圖
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/gameoverbg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖遊戲結背景圖
CCTextureCache::sharedTextureCache()->addImageAsync("game/coin.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 此圖金幣圖示
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/stats_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 勝利介面背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/statstip.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 勝利介面提示按鈕
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/research_bg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 能力提升介面的背景圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/lvSyspng.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 能力提升介面的按鈕圖片
CCTextureCache::sharedTextureCache()->addImageAsync("game/lvinfo.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 能力提升介面的說明文字圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/outdown.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 遊戲結束按鈕圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/outup.png", this, callfuncO_selector(LoadingScene::loadCallBack));
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/updwon.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 升級系統圖片
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/upup.png", this, callfuncO_selector(LoadingScene::loadCallBack));
}
載入資源後進度的計算
void LoadingScene::loadCallBack(CCObject* object)
{
count++;
CCProgressTimer* pt = (CCProgressTimer*)this->getChildByTag(3);
int percentage = (int)count * 100 / total;
char tem[32];
sprintf(tem, "Loading %d", percentage);
CCLOG(tem);
label->setString(tem);
pt->setPercentage(percentage);
CCProgressTimer* ptRadian = (CCProgressTimer*)this->getChildByTag(98);
ptRadian->setPercentage(percentage);
if (count == total)
{
//載入完成就切換場景
CCScene* hello = HelloWorld::scene();
CCTransitionMoveInL* effect = CCTransitionMoveInL::create(2.0f, hello);
CCDirector::sharedDirector()->replaceScene(effect);
}
}
效果:
載入過程
載入完成切換場景
相關文章
- ajax進度條 非同步下載進度條非同步
- 前端資源預載入並展示進度條前端
- 製作遊戲載入進度條遊戲
- Linux 進度條(非100%)列出unzip進度Linux
- 超酷jQuery進度條載入動畫集合jQuery動畫
- CSS快遞進度條效果CSS
- 【新特性速遞】進度條,進度條,進度條
- 常見的網頁載入進度條網頁
- iOS UIWebView載入時新增進度條01iOSUIWebView
- jQuery載入進度條例項程式碼jQuery
- C# 下載帶進度條程式碼(普通進度條)C#
- 報表載入大資料時顯示進度條大資料
- 如何實現圖片預載入和載入進度條
- jQuery環形旋轉載入進度條效果jQuery
- CSS3 螺紋載入進度條效果CSSS3
- 專案需求討論-WebView進度載入條WebView
- js百分比載入進度條效果JS
- Vue canvas繪製圓形進度條動畫載入VueCanvas動畫
- JQuery UI之進度條——ProgressbarjQueryUI
- Glide載入進度IDE
- service worker 對靜態資源進行快取快取
- ArcGIS API for Silverlight 地圖載入進度條類之MapProgressBarAPI地圖APP
- 讀取檔案大小-列印進度條
- vue中頁面載入進度條效果的實現Vue
- Silverlight 系統初始載入進度條美化
- 《前端實戰總結》之使用pace.js為你的網站新增載入進度條前端JS網站
- Python之程式碼進度條Python
- SpringBoot整合Canal進行資料庫 快取同步Spring Boot資料庫快取
- Qt 進度條QT
- HttpWebChilent上傳與下載進度條HTTPWeb
- Ajax 處理時進度條使用
- 使用Java高速實現進度條Java
- 關於WPF進度條的使用
- Xamarin XAML語言教程使用方法設定進度條進度
- 使用 useLazyFetch 進行非同步資料獲取非同步
- 使用Redis和Java進行資料庫快取RedisJava資料庫快取
- Python展示檔案下載進度條Python
- HTML <progress> 進度條HTML