肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026
做什么事都的持之以恒, 距离我写第一篇博文已经过了2天了. 我果然是比较懒的, 哈哈 博文地址:
http://blog.csdn.net/xiao0026/article/details/9001493
CCProgressTimer, 先看效果:
实现这个效果很简单:
CCProgressTimer* pt = CCProgressTimer::create(CCSprite::create("Icon-72.png")); pt->setType( kCCProgressTimerTypeRadial ); //设置状态,我cocos2d-x版本2.13 this->addChild(pt); pt->setPosition( ccp(size.width/2, size.height/2) ); pt->runAction(CCProgressTo::create(3, 100)); //CCProgressTo::create() 第一个参数为持续时间, 第二个参数为最后图像的百分比 //type typedef enum { /// Radial Counter-Clockwise kCCProgressTimerTypeRadial, /// Bar kCCProgressTimerTypeBar, } CCProgressTimerType;
相信大家看的这里肯定心里不是滋味, 哈哈 。 就这么点,还要你说啊。 下面我带大家进入实战项目,演练一个, 还是拿我手上的这个棋牌项目来做例子
一样,先来看看效果。
下班了,大家等等哈。
继续, 直接上代码吧
// // FMCountdownRound.h // 21PointCasual // // Created by Cooki on 12-6-4. // Copyright (c) 2012年 FiveMinutes. All rights reserved. // #ifndef _1PointCasual_FMCountdownRound_h #define _1PointCasual_FMCountdownRound_h #include "cocos2d.h" USING_NS_CC; class FMCountdownRound : public CCSprite { public: virtual bool init(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator); void starCount(); void endCount(); static FMCountdownRound* create(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator=false); static FMCountdownRound* createBycooki(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator=false, float percent = 100.0f); private: void initData(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector); void updateCountdown(); void getRed(); void getGreen(); void getBlue(); void updateColor(); void setPercentage(float percent); private: CCNode* m_target; SEL_MenuHandler m_selector; int m_countdownTime; float m_addPercentage; float m_percentage; CCSprite *m_sprite; unsigned char m_red; unsigned char m_green; unsigned char m_blue; bool m_vabirate; float m_vabirateTime; float m_updateTime; }; #endif
// // FMCountdownRound.cpp // 21PointCasual // // Created by Cooki on 12-6-4. // Copyright (c) 2012年 FiveMinutes. All rights reserved. // #include "FMCountdownRound.h" #include "PlayerView.h" //#include "IosUtile.h" #define START_RED 90 #define START_GREEN 255 #define START_BLUE 25 enum{ tag_progress_bg=0, tag_progress_timer, }; void FMCountdownRound::initData(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector){ m_red = START_RED; m_green = START_GREEN; m_blue = START_BLUE; m_percentage = 100.0f; m_countdownTime = countdownTime; m_addPercentage = 0.5f ; this->m_target = target; this->m_selector = selector; // CCSprite* ptBg = CCSprite::create(backGroundImage); // ptBg->setPosition(CCPointZero); // addChild(ptBg); m_sprite = CCSprite::create(image); m_sprite->setColor(ccc3(m_red, m_green, m_blue)); CCProgressTimer* pt = CCProgressTimer::create(m_sprite); pt->setPosition(CCPointZero); pt->setType( kCCProgressTimerTypeRadial ); pt->setReverseProgress(true); pt->setTag(tag_progress_timer); pt->setPercentage(m_percentage); m_sprite->setAnchorPoint(ccp(0.5f, 0.5f)); m_sprite->setPosition(ccp(m_sprite->getContentSize().width/2, m_sprite->getContentSize().height/2)); addChild(pt); } void FMCountdownRound::starCount(){ m_updateTime = m_countdownTime/m_percentage*m_addPercentage; schedule(schedule_selector(FMCountdownRound::updateCountdown), m_updateTime); } void FMCountdownRound::endCount(){ unschedule(schedule_selector(FMCountdownRound::updateCountdown)); if (m_target){ (m_target->*m_selector)(this); } //if parent is PlayerView ((PlayerView*)getParent())->setCountdownNULL(); removeFromParentAndCleanup(true); } //递增,转圈结束时为255 void FMCountdownRound::getRed(){ m_red = START_RED + (100.f-m_percentage)*(255-START_RED)/100.f; } void FMCountdownRound::getGreen(){ m_green = START_GREEN - (100.f-m_percentage)*(START_GREEN)/100.f; } void FMCountdownRound::getBlue(){ } void FMCountdownRound::updateColor(){ getRed(); getGreen(); getBlue(); } void FMCountdownRound::updateCountdown(){ if (m_vabirate) { m_vabirateTime -= m_updateTime; if (m_vabirateTime < 8.f) { m_vabirate = false; // IosUtile::PhoneVibrator(); } } CCProgressTimer* temPt = (CCProgressTimer*)getChildByTag(tag_progress_timer); m_percentage -= m_addPercentage; if (m_percentage <= 0) { m_percentage = 0; // temPt->setPercentage(m_percentage); endCount(); }else{ // CCLog("percent is: %f, r:%d, g:%d, b:%d" , m_percentage, m_red, m_green, m_blue); updateColor(); m_sprite->setColor(ccc3(m_red, m_green, m_blue)); } temPt->setPercentage(m_percentage); } bool FMCountdownRound::init(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator){ if(!initWithTexture(NULL, CCRectZero)){ return false; } m_vabirate = false; if (needVabirator && countdownTime > 10) { m_vabirate = true; m_vabirateTime = countdownTime; } initData(countdownTime,backGroundImage,image,target,selector); return true; } FMCountdownRound* FMCountdownRound::create(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator){ FMCountdownRound *pRet = new FMCountdownRound(); if (pRet && pRet->init(countdownTime,backGroundImage,image,target,selector, needVabirator)){ pRet->autorelease(); return pRet; }else{ CC_SAFE_DELETE(pRet); return NULL; } } FMCountdownRound* FMCountdownRound::createBycooki(int countdownTime,const char* backGroundImage, const char* image, CCNode* target, SEL_MenuHandler selector, bool needVabirator, float percent){ FMCountdownRound *pRet = new FMCountdownRound(); if (pRet && pRet->init(countdownTime,backGroundImage,image,target,selector, needVabirator)){ pRet->setPercentage(percent); pRet->autorelease(); return pRet; }else{ CC_SAFE_DELETE(pRet); return NULL; } } void FMCountdownRound::setPercentage(float percent){ m_percentage = 100.0f - percent*100.0f; }