cocos2d-x学习(5)-------CCProgressTimer(进度条)

进度条--------CCProgressTimer:

    创建了二个进度条,一个水平进度条,一个圆形进度条,特别是第二个圆形进度条,可以用来表示DOTA技能


1.效果图片:

cocos2d-x学习(5)-------CCProgressTimer(进度条)_第1张图片            cocos2d-x学习(5)-------CCProgressTimer(进度条)_第2张图片


2.源码:

class HelloWorld : public cocos2d::CCLayer
{
protected:
    cocos2d::CCProgressTimer* progress1;
	float  cu1;
    cocos2d::CCProgressTimer* progress2;
	float  cu2;
	cocos2d::CCLabelTTF* numsTTF;
	int opacity;

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();
    
	void update(float dt);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {

        CC_BREAK_IF(! CCLayer::init());

		CCSprite *progressbgSprite=CCSprite::create("progress_bg.png"); 
		progressbgSprite->setAnchorPoint(ccp(0,0));
		progressbgSprite->setPosition(ccp(100, 250));  
		this->addChild(progressbgSprite, 1);  
     
     
		CCSprite *progressSprite=CCSprite::create("progress_1.png");    
		progress1=CCProgressTimer::create(progressSprite);  
		progress1->setType(kCCProgressTimerTypeBar);  
		progress1->setAnchorPoint(ccp(0,0));
		progress1->setPosition(ccp(100, 250));  
     
		//进度动画运动方向,可以多试几个值,看看效果  
		progress1->setMidpoint(ccp(0, 0));  
     
		 //进度条宽高变化  
		progress1->setBarChangeRate(ccp(1, 0));  
		progress1->setPercentage(0);       
		this->addChild(progress1, 1);  


		numsTTF=CCLabelTTF::create("0", "Thonburi", 18);  
		numsTTF->setPosition(ccp(250, 270));  
		this->addChild(numsTTF, 1); 



		CCSprite *progress2Sprite=CCSprite::create("HelloWorld.png");     
		progress2=CCProgressTimer::create(progress2Sprite);    
		 //类型为圆形  
		progress2->setType(kCCProgressTimerTypeRadial);     
		 progress2->setPosition(ccp(250, 150));     
		progress1->setPercentage(0); 
		progress2->setScale(0.5);
		 //0 透明  255完全不透明
		opacity = 255;
		progress2->setOpacity(opacity);
		this->addChild(progress2, 0);  

	this->scheduleUpdate();  

	bRet = true;
    } while (0);

    return bRet;
}



void HelloWorld::update(float dt)
{
	cu1=progress1->getPercentage();  
	if(cu1 >= 100)
	{
		cu1 = 0;
	}
    cu1=cu1+0.1f;  
    progress1->setPercentage(cu1);  
      
      
    CCString *str = CCString::createWithFormat("%.2f%%",cu1);  
    numsTTF->setString(str->getCString());  
   

	cu2=progress2->getPercentage();  
	if(cu2 >= 100)
	{
		cu2 = 0;
	}
    cu2=cu2+0.5f;  
    progress2->setPercentage(cu2);  

	if(opacity == 0 )
	{
		opacity = 256;
	}
	opacity = opacity - 1;

	progress2->setOpacity(opacity);

}


3.参考

1.http://blog.csdn.net/kuloveyouwei/article/details/9062687


你可能感兴趣的:(cocos2d-x,CCProgressTimer)