CCProgressTimerType类型如下:
typedef enum {
/// Radial Counter-Clockwise
kCCProgressTimerTypeRadial,
/// Bar
kCCProgressTimerTypeBar,
} CCProgressTimerType;
实现从左到右的进度条:
pt->setMidpoint(ccp(0,0));
pt->setBarChangeRate(ccp(1,0));
具体实现如下: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); } }