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); }
事實上思路都差點兒相同啦,大概就是依據( 已經載入的圖片數 / 總圖片資源數)百分比來算出進度條的百分比來滑動,或者幹脆把進度條最大值設置成圖片資源總數。載入多少就滑動多少。。
==================================
恩。就這樣子吧。好困的夜晚。。晚安
cocos2d-x 3.0 Loading界面實現