使用cocos2d-x 2.0.1 写的进度条例子。 ~~ 以后可以用来做个进度组件

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。

 

下面是运行后的效果图:

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八_戒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值