Cocos2dx之進度條使用,非同步載入資源進快取

君墨痕發表於2014-03-14

在遊戲前通常都會載入好遊戲中需要用到的資源,遊戲的時候繪圖才不會讓記憶體飆升,不那麼卡頓,

載入資源存到快取時有一點點需要等待的時間,那麼就設定一個場景顯示一下進度條,載入完成就切換場景進入主遊戲

進度條所在的標頭檔案

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);
	}
}

效果:

載入過程



載入完成切換場景



相關文章