原帖地址
http://blog.sina.com.cn/s/blog_63cf1c510101bo56.html
cocos2d-x提供了CCProgressTimer類可以方便地實現進度條的功能,cocos2d-x 1.x版本與cocos2d-x 2.x版本對ProgressTimer的類型提供的不同,2.x版本只提供了以下2種:
typedef enum { /// Radial Counter-Clockwise kCCProgressTimerTypeRadial, /// Bar kCCProgressTimerTypeBar, } CCProgressTimerType;
另外,cocos2d-iphone與cocos2d-x除了實現語言不同外,還有很大的差異,比如cocos2d-x初始化一個實例時用的是create()函數,cocos2d用的是各種initwithxxx,cocos2d中的OC語法中的存取器容易誤導C++出身的程序員,例如點操作符表示調用getter或setter方法,而不是C++中的訪問public數據成員。
例子中代碼功能爲:
虛擬一個資源加載的進度條,一紅一綠,分別增加和減少,加載完畢後,會運行一個顏色漸變的切換場景,然後進入主場景,具體效果請看以下URL:
http://www.56.com/u88/v_OTE0MTgwODU.html
以下爲具體代碼,老習慣,不碼字,看註釋:
1. 頭文件,其中有2個私有數據成員,分別爲進度條1和進度條2:
#include "cocos2d.h" class LoadGame : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); void LoadGame::update(float dt); // implement the "static node()" method manually CREATE_FUNC(LoadGame); private: cocos2d::CCProgressTimer *progresstime1; cocos2d::CCProgressTimer *progresstime2; }; #endif // __HELLOWORLD_SCENE_H__
2. LoadGame類具體實現
//創建二個精靈,一綠一紅 CCSprite *psSprite1 = CCSprite::create("green.png"); CCSprite *psSprite2 = CCSprite::create("red.png"); //利用精靈創建進度條,並設置一些屬性 progresstime1 = CCProgressTimer::create(psSprite1); //初始化CCProgressTimer progresstime1->setPercentage(0.0f); //設置初始百分比的值 progresstime1->setScale(3); //設置進度條大小爲原始的3倍 progresstime1->setBarChangeRate(ccp(1, 0)); //設置進度條的長度和高度開始變化的大小 progresstime1->setType(kCCProgressTimerTypeBar); //設置進度條爲水平 progresstime1->setPosition(ccp(size.width/2, size.height/2)); //放置進度條位置 this->addChild(progresstime1, 100); //加入Layer中 //利用精靈創建進度條,並設置一些屬性 progresstime2 = CCProgressTimer::create(psSprite2); //初始化CCProgressTimer progresstime2->setPercentage(100.0f); //設置初始百分比的值 progresstime2->setScale(3); //設置進度條大小爲原始的3倍 progresstime2->setBarChangeRate(ccp(1, 0)); //設置進度條的長度和高度開始變化的大小 progresstime2->setType(kCCProgressTimerTypeBar); //設置進度條爲水平 progresstime2->setPosition(ccp(size.width/2, size.height/2 - 30)); //放置進度條位置 this->addChild(progresstime2, 101); //加入Layer中 this->scheduleUpdate(); //調用定時器更新進度條
3. 定時器的具體實現:
void LoadGame::update(float dt) { //CCProgressTimer *progresstime = static_cast(this->getChildByTag(100)); float ct1 = progresstime1->getPercentage(); //取得當前進度的百分比 float ct2 = progresstime2->getPercentage(); //取得當前進度的百分比 ct1 = ct1 + 0.5f; //每幀+0.5% ct2 = ct2 - 0.5f; //如果進度條小於100%,設置進度條的百分比 if (ct1 <= 100) { CCLOG("progresstime1:%f, progresstime2:%f", ct1, ct2); progresstime1->setPercentage(ct1); progresstime2->setPercentage(ct2); } //如果進度條達到100%,則進入過渡場景,過渡場景會在2秒後進入主場景 else { CCTransitionFade *tScene = CCTransitionFade::create(2, HelloWorld::scene(), ccWHITE); CCDirector::sharedDirector()->replaceScene(tScene); } }
以下三個場景過渡特效需要檢測opengl版本是否支持:
CCConfiguration::sharedConfiguration()->getGlesVersion() <= GLES_VER_1_0如果爲真則爲不支持
- CCTransitionCrossFade::transitionWithDuration(t,s);//淡出淡入交叉,同時進行
- CCTransitionRadialCCW::transitionWithDuration(t,s);//順時針切入
- CCTransitionRadialCW::transitionWithDuration(t,s);//逆時針切入