1. 程式人生 > >cocos2d-x 菜鳥學習筆記三(圖片預載入與進度條)

cocos2d-x 菜鳥學習筆記三(圖片預載入與進度條)

在做遊戲時,會用於很多和圖片資源,包括角色,動畫,紋理貼圖……為了減少GPU和CPU的快取佔用以及圖片的重複利用,在遊戲開始時,都會預載入這些資源進入快取,在cocos2d-x裡用的是CCTextureCache這個類。其實,在cocos2d-x自帶的TestCpp裡有對應的例子,在裡面用的是:

[cpp] view plaincopyprint?
  1. addImageAsync(constchar *path, CCObject *target, SEL_CallFuncO selector); 
	addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector);
[cpp] view plaincopyprint?
  1. CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_01.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack)); 
CCTextureCache::sharedTextureCache()->addImageAsync("Images/grossini_dance_01.png", this, callfuncO_selector(TextureCacheTest::loadingCallBack));

這個方法通過圖片的路徑path,載入入target如:某個scene,當這個圖片載入完成後,會呼叫一個回撥函式selector,而這個回撥函式便可以用於更新圖片資料載入的總進度。關於紋理的介紹推薦一篇文章:http://www.oschina.net/question/565065_79823

進度條在網上的教程已經介紹得很清楚了,只是最近更新的版本重寫了程式碼,只保留了兩種進度條顯示的基礎型別,但是在引用這兩種基礎型別時,通過設定進度條的引數,還是可以變換出更多的顯示效果的,這些效果在TestCPP中的ActionsProgressTest可以完全看到,使用的CCProgressTimer,下面使用的就是橫向進度條的顯示程式碼:

[cpp] view plaincopyprint?
  1. CCProgressTimer *left = CCProgressTimer::create(CCSprite::create(s_pPathSister1)); 
  2.     left->setType(kCCProgressTimerTypeBar); 
  3.     //    Setup for a bar starting from the left since the midpoint is 0 for the x
  4.     left->setMidpoint(ccp(0,0)); 
  5.     //    Setup for a horizontal bar since the bar change rate is 0 for y meaning no vertical change
  6.     left->setBarChangeRate(ccp(1, 0)); 
  7.     //用來設定進度條的進度
  8.     left->setPercentage(20); 
  9.     addChild(left); 
  10.     left->setPosition(ccp(100, s.height/2)); 
CCProgressTimer *left = CCProgressTimer::create(CCSprite::create(s_pPathSister1));
    left->setType(kCCProgressTimerTypeBar);
    //    Setup for a bar starting from the left since the midpoint is 0 for the x
    left->setMidpoint(ccp(0,0));
    //    Setup for a horizontal bar since the bar change rate is 0 for y meaning no vertical change
    left->setBarChangeRate(ccp(1, 0));
    //用來設定進度條的進度
    left->setPercentage(20);
    addChild(left);
    left->setPosition(ccp(100, s.height/2));

setType(kCCProgressTimerTypeBar);

用來設定進度條的顯示型別

kCCProgressTimerTypeBar

可以看作是按矩形顯示效果的進度條型別

setMidpoint

用來設定進度條橫向前進的方向從左向右或是從右向左

setBarChangeRate

用來設定進度條增長按橫向或是按縱向增長

最後,只要把設定進度條的方法寫到回撥函式中便可即時更新載入資源時進度條的顯示進度。

另一個顯示效果是圓周效果的進度條顯示kCCProgressTimerTypeRadial,可以通過setReverseProgress(true);方法來設定按逆時針顯示進度,False則為順時針顯示。