cocos2d-x 3.0 Loading界面實現


這個世界每一天都在驗證我們的渺小,但我們卻在努力創造,不斷的在這生活的畫卷中留下自己的腳印,也許等到我們老去的那一天,老得不能動只能靠回憶的那一天,你躺在輪椅上,不斷的回憶過去,相思的痛苦忘不了,相戀的甜蜜浮現在心頭,嘴角不覺一笑,年少時的瘋狂,熱情,理想和抱負,都隨着歲月的流去而化作人生的財富,或多或少,猶如那夕陽西下的餘輝,在慢慢消失着不見。。


(不文藝你會死?)


好吧,最近天天在忙着寫遊戲,天天寫,而且效率還不高,光這兩天想着怎麼優化和控制敵人出現的邏輯和地圖數據的存儲,就前前後後墨跡了倆天才寫完,好吧,要嚴重反思一下。總結一下如何提高寫代碼的效率:寫代碼的時候不要刷微博,寫代碼的時候不要開qq,寫代碼的時候不要上知乎。

恩,就這樣子!


=========================================================


以上純屬扯淡,恩,扯淡。。。(說廢話好玩嗎?)


我們在玩遊戲的經常會遇到loading界面,顧名思義,主要用來加預先載資源文件。先來一張效果圖:


恩,有點醜,不過大概就這樣子哈,加載完成後開啓遊戲界面


其實這就是一張背景圖片 + 一個進度條  + 跟隨進度條提示的小框框 + 一個loading文字標籤


恩,我們來模擬實現它,首先,我們創建這些資源

	auto winSize = Director::getInstance()->getWinSize();
	
	//背景圖片
	auto background = Sprite::create(IMG_LOADINGBG);
	background->setPosition(winSize.width / 2, winSize.height / 2);
	this->addChild(background,0);

	//loading文字標籤
	_loadingLabel = LabelTTF::create("Loading...", "Arial", 25);
	_loadingLabel->setPosition(winSize.width / 2, winSize.height / 2 - 50);
	this->addChild(_loadingLabel);

	//ControlSlider進度條
	_curProgress = 0;
	_progressBar = ControlSlider::create("bar_bg.png","bar.png","bar_thumb.png");
	_progressBar->setPosition(winSize.width / 2, winSize.height / 2);
	_progressBar->setTouchEnabled(false);
	_progressBar->setMinimumValue(0);
	_progressBar->setMaximumValue(100);
	_progressBar->setValue(0);
	this->addChild(_progressBar,1);

	//進度條上面的提示小框框
	_barTip = Sprite::create(IMG_LOADING_BAR_TIP);
	_barTip->setPosition(
		winSize.width / 2 -  _progressBar->getContentSize().width / 2,
		winSize.height / 2 + 50);
	this->addChild(_barTip,1);

	//提示小框框文字標籤
	_barTipLabel = LabelTTF::create("0%", "Arial", 20);
	_barTipLabel->setPosition(
		_barTip->getContentSize().width / 2,_barTip->getContentSize().height / 2
		);

	_barTip->addChild(_barTipLabel);

然後讓它動起來,我們讓它執行一百次,哈哈

        //interval,repeat,delay
	this->schedule(schedule_selector(LoadingScene::loadingLogic),1.0 / 100 ,100,0.2f);

每次執行動態更新提示框的位置和文字標籤的信息,到了第一百次,就開啓另外一個界面,恩,就是這麼簡單。。

void LoadingScene::loadingLogic(float dt){
	_curProgress ++;
	if(_curProgress > 100){
		//begin the game choose scene
		Director::getInstance()->replaceScene(TransitionFade::create(0.5f,ChooseScene::createScene()));
		return;
	}
	_progressBar->setValue(_curProgress);

	int startX = _progressBar->getPositionX() - _progressBar->getContentSize().width / 2 +10 ;
	int unitX = _progressBar->getContentSize().width / 100;

	_barTip->setPositionX(startX + _curProgress * unitX);
	char str[10] = {0};
	sprintf(str,"%d%",_curProgress);

	_barTipLabel->setString(str);

}

咦,看到這裏有沒有覺得哪裏不對?好吧,被你發現了,說好的資源加載哪裏去了?而且資源加載的進度百分比怎麼算的呢?


好吧,繼續,比如我們有一百張圖片資源。。。(爲什麼不是99張?)


void LoadingScene::onEnter(){
	Layer::onEnter();
	//加載一次圖片資源就回調一次
	Director::getInstance()->getTextureCache()->addImageAsync("1.png",this,callfunc_selector(LoadingScene::loadingCallback));
	...
	...
	...
	Director::getInstance()->getTextureCache()->addImageAsync("100.png",this,callfunc_selector(LoadingScene::loadingCallback));
	
}

然後回調函數實現,每次執行動態更新提示框的位置和文字標籤的信息,到了第一百次,就開啓另外一個界面,恩,還是這麼簡單。。。


void LoadingScene::loadingCallback(){
	_curProgress ++;
	if(_curProgress > 100){
		//begin the game choose scene
		Director::getInstance()->replaceScene(TransitionFade::create(0.5f,ChooseScene::createScene()));
		return;
	}
	_progressBar->setValue(_curProgress);

	int startX = _progressBar->getPositionX() - _progressBar->getContentSize().width / 2 +10 ;
	int unitX = _progressBar->getContentSize().width / 100;

	_barTip->setPositionX(startX + _curProgress * unitX);
	char str[10] = {0};
	sprintf(str,"%d%",_curProgress);

	_barTipLabel->setString(str);

}

其實思路都差不多啦,大概就是根據( 已經加載的圖片數 /  總圖片資源數)百分比來算出進度條的百分比來滑動,或者乾脆把進度條最大值設置成圖片資源總數,加載多少就滑動多少。。


==================================


恩,就這樣子吧,好睏的夜晚。。晚安


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章