带冷却效果的菜单按钮CCMenuItem
//
// LQCoolingSprite.h
//
// 带冷却效果的按钮
//
#ifndef __LQCoolingSprite_H__
#define __LQCoolingSprite_H__
#include "cocos2d.h"
using namespace cocos2d;
/**
cdTime:冷却时间
button_normal_name,button_click_name 菜单资源
target,selector 事件
LQCoolingSprite *button = LQCoolingSprite::create(10.f, "btn_bb.png", "btn_bb.png",
this,menu_selector(LQCityCtrlLayer::btnPressed) );
button->setPosition(ccp(this->getContentSize().width/2,300));
m_pMenu->addChild(button,20);
*/
class LQCoolingSprite : public cocos2d::CCMenuItemSprite
{
private:
CCObject* m_ptargetListener;
SEL_MenuHandler m_ptargetSelector;
cocos2d::CCSprite* mStencil; // 蒙板精灵,黑色半透明(这个是为了显示一个冷却计时效果)
cocos2d::CCProgressTimer* mProgressTimer; // 时间进度条精灵(360度旋转)
//初始化
bool init(float cdTime, const char* button_normal_name, const char* button_click_name, cocos2d::CCObject *target, SEL_MenuHandler selector);
public:
/** 创建一个LQCoolingSprite对象
*/
static LQCoolingSprite* create(float cdTime,
const char* button_normal_name,
const char* button_click_name,
cocos2d::CCObject *target,
SEL_MenuHandler selector);
LQCoolingSprite();
~LQCoolingSprite();
//CD时间设置
CC_SYNTHESIZE(float, mCDTime, CDTime)
//技能按钮点击回调
void clickCallBack(cocos2d::CCObject* obj);
//技能冷却完毕回调
void coolDownCallBack(cocos2d::CCNode* node);
};
#endif
//
// LQCoolingSprite.cpp
//
#include "LQCoolingSprite.h"
using namespace cocos2d;
LQCoolingSprite* LQCoolingSprite::create(float cdTime, const char* button_normal_name, const char* button_click_name,cocos2d::CCObject *target, SEL_MenuHandler selector)
{
LQCoolingSprite* button = new LQCoolingSprite();
if (button && button->init(cdTime, button_normal_name, button_click_name, target, selector))
{
button->autorelease();
return button;
}
else
{
delete button;
button = NULL;
}
return NULL;
}
LQCoolingSprite::LQCoolingSprite():
mStencil(NULL),
mProgressTimer(NULL),
mCDTime(1.f)
{
CCLOG("LQCoolingSprite---构造函数");
}
LQCoolingSprite::~LQCoolingSprite()
{
CCLOG("~LQCoolingSprite---析构函数");
}
bool LQCoolingSprite::init(float cdTime, const char* button_normal_name, const char* button_click_name, cocos2d::CCObject *target, SEL_MenuHandler selector)
{
CCAssert(button_normal_name, "LQCoolingSprite::init button_normal_name != NULL");
CCAssert(button_click_name, "LQCoolingSprite::init button_click_name != NULL");
CCNode *normalSprite = CCSprite::createWithSpriteFrameName(button_normal_name);
CCNode *selectedSprite = CCSprite::createWithSpriteFrameName(button_click_name);
m_ptargetListener = target;
m_ptargetSelector = selector;
if(!CCMenuItemSprite::initWithNormalSprite(normalSprite, selectedSprite, NULL, this, menu_selector(LQCoolingSprite::clickCallBack) ))
return false;
// Notice:添加child时要注意上下层
// 最下方是CCMenuItemImage 其次是模版图片 最上方是CCProgressTimer
// 添加阴影模版
//mStencil = CCSprite::create(stencil_file_name);
mStencil = CCSprite::create("blank.png");
mStencil->setTextureRect( CCRectMake(0,0,this->getContentSize().width,this->getContentSize().height) );
mStencil->setOpacity(180);
mStencil->setColor(ccc3(50,50,50));
mStencil->setPosition(ccpSize(this->getContentSize(),0.5));
mStencil->setVisible(false);
addChild(mStencil);
// 添加旋转进度条精灵
CCSprite* progressSprite = CCSprite::createWithSpriteFrameName(button_normal_name);
mProgressTimer = CCProgressTimer::create(progressSprite);
mProgressTimer->setPosition(CCPointZero);
mProgressTimer->setVisible(false);
mProgressTimer->setPosition(ccpSize(this->getContentSize(),0.5));
addChild(mProgressTimer, 100);
mCDTime = cdTime;
return true;
}
/** 技能按钮点击回调 */
void LQCoolingSprite::clickCallBack(cocos2d::CCObject* obj)
{
//执行设置的动作
if (m_ptargetListener && m_ptargetSelector)
{
(m_ptargetListener->*m_ptargetSelector)(this);
}
// 冷却计时,即时状态技能按钮不可点击
this->setEnabled(false);
// 模版可见
mStencil->setVisible(true);
// 设置精灵进度条为顺时针
mProgressTimer->setVisible(true);
mProgressTimer->setType(kCCProgressTimerTypeRadial);
//准备一个5秒旋转360度的动画(逐渐覆盖半透模板形成冷却效果;这里进行计时冷却动画的实现和时间控制)
CCActionInterval* action_progress_to = CCProgressTo::create(mCDTime, 100);
CCCallFunc* action_callback = CCCallFuncN::create(this, callfuncN_selector(LQCoolingSprite::coolDownCallBack));
mProgressTimer->runAction(CCSequence::create(action_progress_to, action_callback, NULL));
}
/** 技能冷却完成回调 */
void LQCoolingSprite::coolDownCallBack(CCNode* node)
{
// 设置蒙板不可见
mStencil->setVisible(false);
// 进度条技能不可见
mProgressTimer->setVisible(false);
// 按钮置为可用
this->setEnabled(true);
}
#define ccpSize(v,s) \
ccp(v.width*s, v.height*s)