GameScene.h
#include "cocos2d.h"
USING_NS_CC;
class GameScene : public cocos2d::CCLayer
{
public:
GameScene(void);
~GameScene(void);
void update(float dt);
private:
float progressNum;
};
GameScene.cpp
#include "GameScene.h"
GameScene::GameScene(void):progressNum(0)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
//进度条的外框
CCSprite *progressBar = CCSprite::create("progress_外框.png");
progressBar->setPosition(ccp(size.width*0.5f,size.height*0.5));
this->addChild(progressBar);
//进度条的内框
CCSprite *progressSprite = CCSprite::create("progress_内进度.png");
CCProgressTimer *timer = CCProgressTimer::create(progressSprite);
timer->setType(kCCProgressTimerTypeBar); //水平
timer->setMidpoint(CCPointZero); //进度的开始的中间位置--》这里设置为(0,0)
timer->setBarChangeRate(ccp(1.0f,0)); //设置进度条的长度和高度开始变化的大小
timer->setPercentage(0); //设置进度百分比
timer->setPosition(ccp(size.width*0.5f,size.height*0.5f));
this->addChild(timer,1,100);
//中部的的进度显示
CCLabelAtlas *progressText = CCLabelAtlas::create("0%","fps_images.png",12, 32, '.');
progressText->setPosition(ccp(size.width*0.5f,size.height*0.5f));
this->addChild(progressText,1,101);
this->scheduleUpdate();
}
GameScene::~GameScene(void)
{
this->unscheduleUpdate();
}
void GameScene::update( float dt )
{
CCProgressTimer *timer = (CCProgressTimer *)this->getChildByTag(100);
CCLabelAtlas *progressText = (CCLabelAtlas *)this->getChildByTag(101);
CCString *str = CCString::stringWithFormat("%.3f",progressNum*100/timer->getContentSize().width);
progressText->setString(str->getCString());
timer->setPercentage(progressNum*100/timer->getContentSize().width);
progressNum +=0.3f;
if(progressNum>timer->getContentSize().width)
{
CCLOG("%f",progressNum);
progressNum = 0;
}
}
比较重要的在代码里有注释,可以看看。 这个进度现实可以用在场景过渡,耗内存大要释放的地方。~~ 我觉得比较ok。
下面是运行后的效果图: