cocos2d-x 之 CCProgressTimer 以及扩展实现颜色渐变进度条等等

转自:    http://blog.csdn.net/cookirui/article/details/9015333



CCProgressTimer, 先看效果:

实现这个效果很简单:


[cpp]  view plain copy
  1. CCProgressTimer* pt = CCProgressTimer::create(CCSprite::create("Icon-72.png"));  
  2.     pt->setType( kCCProgressTimerTypeRadial );  //设置状态,我cocos2d-x版本2.13   
  3.     this->addChild(pt);  
  4.     pt->setPosition( ccp(size.width/2, size.height/2) );  
  5.     pt->runAction(CCProgressTo::create(3, 100)); //CCProgressTo::create() 第一个参数为持续时间, 第二个参数为最后图像的百分比  
  6.       
  7.       
  8.     //type  
  9.     typedef enum {  
  10.         /// Radial Counter-Clockwise  
  11.         kCCProgressTimerTypeRadial,  
  12.         /// Bar  
  13.         kCCProgressTimerTypeBar,  
  14.     } CCProgressTimerType;  


相信大家看的这里肯定心里不是滋味, 哈哈 。 就这么点,还要你说啊。 下面我带大家进入实战项目,演练一个, 还是拿我手上的这个棋牌项目来做例子


一样,先来看看效果


下班了,大家等等哈。


继续,  直接上代码吧

[cpp]  view plain copy
  1. //  
  2. //  FMCountdownRound.h  
  3. //  21PointCasual  
  4. //  
  5. //  Created by Cooki on 12-6-4.  
  6. //  Copyright (c) 2012年 FiveMinutes. All rights reserved.  
  7. //  
  8.   
  9. #ifndef _1PointCasual_FMCountdownRound_h  
  10. #define _1PointCasual_FMCountdownRound_h  
  11.   
  12. #include "cocos2d.h"  
  13. USING_NS_CC;  
  14.   
  15. class FMCountdownRound : public CCSprite {  
  16. public:  
  17.     virtual bool init(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator);  
  18.     void starCount();  
  19.     void endCount();  
  20.     static FMCountdownRound* create(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator=false);  
  21.     static FMCountdownRound* createBycooki(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator=falsefloat percent = 100.0f);  
  22. private:  
  23.     void initData(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector);  
  24.     void updateCountdown();  
  25.     void getRed();  
  26.     void getGreen();  
  27.     void getBlue();  
  28.     void updateColor();  
  29.     void setPercentage(float percent);  
  30. private:  
  31.     CCNode* m_target;   
  32.     SEL_MenuHandler m_selector;  
  33.     int m_countdownTime;  
  34.     float m_addPercentage;  
  35.     float m_percentage;  
  36.     CCSprite *m_sprite;  
  37.     unsigned char m_red;  
  38.     unsigned char m_green;  
  39.     unsigned char m_blue;  
  40.     bool m_vabirate;  
  41.     float m_vabirateTime;  
  42.     float m_updateTime;  
  43.   
  44. };  
  45.   
  46. #endif  


[cpp]  view plain copy
  1. //  
  2. //  FMCountdownRound.cpp  
  3. //  21PointCasual  
  4. //  
  5. //  Created by Cooki on 12-6-4.  
  6. //  Copyright (c) 2012年 FiveMinutes. All rights reserved.  
  7. //  
  8.   
  9. #include "FMCountdownRound.h"  
  10. #include "PlayerView.h"  
  11. //#include "IosUtile.h"  
  12.   
  13. #define START_RED 90  
  14. #define START_GREEN 255  
  15. #define START_BLUE 25  
  16.   
  17. enum{  
  18.     tag_progress_bg=0,  
  19.     tag_progress_timer,  
  20. };  
  21. void FMCountdownRound::initData(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector){  
  22.   
  23.     m_red = START_RED;  
  24.     m_green = START_GREEN;  
  25.     m_blue = START_BLUE;  
  26.     m_percentage = 100.0f;  
  27.     m_countdownTime = countdownTime;  
  28.     m_addPercentage = 0.5f ;  
  29.   
  30.     this->m_target = target;  
  31.     this->m_selector = selector;  
  32.       
  33. //    CCSprite* ptBg = CCSprite::create(backGroundImage);  
  34. //    ptBg->setPosition(CCPointZero);  
  35. //    addChild(ptBg);  
  36.       
  37.     m_sprite = CCSprite::create(image);  
  38.     m_sprite->setColor(ccc3(m_red, m_green, m_blue));  
  39.       
  40.     CCProgressTimer* pt = CCProgressTimer::create(m_sprite);  
  41.     pt->setPosition(CCPointZero);  
  42.     pt->setType( kCCProgressTimerTypeRadial );  
  43.     pt->setReverseProgress(true);  
  44.     pt->setTag(tag_progress_timer);  
  45.     pt->setPercentage(m_percentage);  
  46.     m_sprite->setAnchorPoint(ccp(0.5f, 0.5f));  
  47.     m_sprite->setPosition(ccp(m_sprite->getContentSize().width/2, m_sprite->getContentSize().height/2));  
  48.     addChild(pt);  
  49. }  
  50.   
  51. void FMCountdownRound::starCount(){  
  52.     m_updateTime = m_countdownTime/m_percentage*m_addPercentage;  
  53.     schedule(schedule_selector(FMCountdownRound::updateCountdown), m_updateTime);  
  54. }  
  55.   
  56. void FMCountdownRound::endCount(){  
  57.     unschedule(schedule_selector(FMCountdownRound::updateCountdown));  
  58.     if (m_target){  
  59.         (m_target->*m_selector)(this);  
  60.     }  
  61.     //if parent is PlayerView  
  62.     ((PlayerView*)getParent())->setCountdownNULL();  
  63.     removeFromParentAndCleanup(true);  
  64. }  
  65.   
  66. //递增,转圈结束时为255  
  67. void FMCountdownRound::getRed(){  
  68.     m_red = START_RED + (100.f-m_percentage)*(255-START_RED)/100.f;  
  69. }  
  70. void FMCountdownRound::getGreen(){  
  71.     m_green = START_GREEN - (100.f-m_percentage)*(START_GREEN)/100.f;  
  72. }  
  73. void FMCountdownRound::getBlue(){  
  74.       
  75. }  
  76.   
  77. void FMCountdownRound::updateColor(){  
  78.     getRed();  
  79.     getGreen();  
  80.     getBlue();  
  81.       
  82. }  
  83.   
  84. void FMCountdownRound::updateCountdown(){  
  85.     if (m_vabirate) {  
  86.         m_vabirateTime -= m_updateTime;  
  87.         if (m_vabirateTime < 8.f) {  
  88.             m_vabirate = false;  
  89. //            IosUtile::PhoneVibrator();  
  90.         }  
  91.     }  
  92.     CCProgressTimer* temPt = (CCProgressTimer*)getChildByTag(tag_progress_timer);  
  93.     m_percentage -= m_addPercentage;  
  94.     if (m_percentage <= 0) {  
  95.         m_percentage = 0;  
  96. //        temPt->setPercentage(m_percentage);  
  97.         endCount();  
  98.     }else{  
  99. //        CCLog("percent is: %f, r:%d, g:%d, b:%d" , m_percentage, m_red, m_green, m_blue);  
  100.         updateColor();  
  101.         m_sprite->setColor(ccc3(m_red, m_green, m_blue));  
  102.     }  
  103.     temPt->setPercentage(m_percentage);  
  104. }  
  105.   
  106. bool FMCountdownRound::init(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator){  
  107.     if(!initWithTexture(NULL, CCRectZero)){  
  108.         return false;  
  109.     }  
  110.       
  111.     m_vabirate = false;  
  112.     if (needVabirator && countdownTime > 10) {  
  113.         m_vabirate = true;  
  114.         m_vabirateTime = countdownTime;  
  115.     }  
  116.     initData(countdownTime,backGroundImage,image,target,selector);  
  117.     return true;  
  118. }  
  119.   
  120. FMCountdownRound* FMCountdownRound::create(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator){  
  121.     FMCountdownRound *pRet = new FMCountdownRound();  
  122.     if (pRet && pRet->init(countdownTime,backGroundImage,image,target,selector, needVabirator)){  
  123.         pRet->autorelease();  
  124.         return pRet;  
  125.     }else{  
  126.         CC_SAFE_DELETE(pRet);  
  127.         return NULL;  
  128.     }  
  129. }  
  130.   
  131. FMCountdownRound* FMCountdownRound::createBycooki(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator, float percent){  
  132.     FMCountdownRound *pRet = new FMCountdownRound();  
  133.     if (pRet && pRet->init(countdownTime,backGroundImage,image,target,selector, needVabirator)){  
  134.         pRet->setPercentage(percent);  
  135.         pRet->autorelease();  
  136.         return pRet;  
  137.     }else{  
  138.         CC_SAFE_DELETE(pRet);  
  139.         return NULL;  
  140.     }  
  141. }  
  142. void FMCountdownRound::setPercentage(float percent){  
  143.     m_percentage = 100.0f - percent*100.0f;  
  144. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值