标签:技能冷却
#pragma once #include "cocos2d.h" USING_NS_CC; class SkillButton :public Node { public: SkillButton(); ~SkillButton(); //技能标记 static SkillButton * createSkillButton(float skillCD,const char* normalImage,const char* clikImage,const char* maskImage,int tag); bool init(float skillCD, const char* normalImage, const char* clikImage, const char* maskImage,int tag); void skillClikCallBack(Ref* obj); //单击回调 void skilCoolDownCallBack(Node* node); /** 技能冷却完毕回调*/ private: MenuItemImage * _itemSkill; // 技能按钮 Menu * _menuSkill; // 技能按钮所在menu Sprite * _mask; // 蒙板精灵,黑色半透明(这个是为了显示一个冷却计时效果) ProgressTimer * _ProgressTimer; // 时间进度条精灵(360度旋转) // CD时间 CC_SYNTHESIZE(float, mCDtime, CDtime); };
#include "SkillButton.h" SkillButton::SkillButton() { _mask = NULL; _itemSkill = NULL; _menuSkill = NULL; _ProgressTimer = NULL; mCDtime = 1.0f; } SkillButton::~SkillButton() { } SkillButton * SkillButton::createSkillButton(float skillCD, const char* normalImage, const char* clikImage, const char* maskImage,int tag) { SkillButton * skill = new SkillButton(); if (skill && skill->init(skillCD,normalImage,clikImage,maskImage,tag)) { skill->autorelease(); return skill; } else { CC_SAFE_DELETE(skill); skill = NULL ; } } bool SkillButton::init(float skillCD, const char* normalImage, const char* clikImage, const char* maskImage,int tag) { // Notice:添加child时要注意上下层 // 最下方是CCMenuItemImage 其次是模版图片 最上方是CCProgressTimer // 添加技能按钮 _itemSkill = MenuItemImage::create(normalImage, clikImage, CC_CALLBACK_1(SkillButton::skillClikCallBack, this)); _itemSkill->setPosition(Vec2::ZERO); _itemSkill->setTag(tag); _menuSkill = Menu::create(_itemSkill,NULL); _menuSkill->setPosition(Vec2::ZERO); this->addChild(_menuSkill); // 添加阴影模版 _mask = Sprite::create(maskImage); _mask->setPosition(Vec2::ZERO); _mask->setVisible(false); _mask->retain(); this->addChild(_mask); // 添加旋转进度条精灵 auto sp = Sprite::create(normalImage); _ProgressTimer = ProgressTimer::create(sp); _ProgressTimer->setPosition(Vec2::ZERO); _ProgressTimer->setVisible(false); this->addChild(_ProgressTimer); this->setCDtime(skillCD); return true; } void SkillButton::skillClikCallBack(Ref* obj) //单击回调 { // 冷却计时,即时状态技能按钮不可点击 _itemSkill->setEnabled(false); // 模版可见 _mask->setVisible(true); // 设置精灵进度条为顺时针 _ProgressTimer->setVisible(true); _ProgressTimer->setType(ProgressTimer::Type::RADIAL); //准备一个5秒旋转360度的动画(逐渐覆盖半透模板形成冷却效果;这里进行计时冷却动画的实现和时间控制) auto action_progrees_to = ProgressTo::create(mCDtime,100); auto action_call_back = CallFuncN::create(CC_CALLBACK_1(SkillButton::skilCoolDownCallBack,this)); _ProgressTimer->runAction(Sequence::create(action_progrees_to,action_call_back,NULL)); MenuItemImage* item = (MenuItemImage*)obj; switch (item->getTag()) { case 1: { CCLOG("item 1"); } break; case 2: { CCLOG("item 2"); } break; case 3: { CCLOG("item 3"); } break; case 4: { CCLOG("item 4"); } break; default: break; } } void SkillButton::skilCoolDownCallBack(Node* node) /** 技能冷却完毕回调*/ { // 设置蒙板不可见 _mask->setVisible(false); // 进度条技能不可见 _ProgressTimer->setVisible(false); //重置进度为0 _ProgressTimer->setPercentage(0); // 按钮置为可用 _itemSkill->setEnabled(true); } 这里调用 技能类 #include "OperateLayer.h" #include "SkillButton.h" OperateLayer::OperateLayer() { } OperateLayer::~OperateLayer() { } bool OperateLayer::init() { if (!Layer::init()) { return false; } auto winSize = Director::getInstance()->getWinSize(); auto _skill_1 = SkillButton::createSkillButton(10, "Spell_Holy_BlessingOfStrength.jpg", "Spell_Holy_BlessingOfStrength.jpg", "stencil.png",1); _skill_1->setPosition(Vec2(winSize.width - 42, 50)); this->addChild(_skill_1); auto _skill_2 = SkillButton::createSkillButton(8,"Spell_Nature_ThunderClap.jpg","Spell_Nature_ThunderClap.jpg","stencil.png",2); _skill_2->setPosition(Vec2(_skill_1->getPositionX() - 74, 50)); this->addChild(_skill_2); auto _skill_3 = SkillButton::createSkillButton(5, "Spell_Fire_FlameTounge.jpg", "Spell_Fire_FlameTounge.jpg", "stencil.png",3); _skill_3->setPosition(Vec2(_skill_2->getPositionX() - 74 , 50)); this->addChild(_skill_3); auto _skill_4 = SkillButton::createSkillButton(3, "Spell_Holy_AuraOfLight.jpg", "Spell_Holy_AuraOfLight.jpg", "stencil.png",4); _skill_4->setPosition(Vec2(_skill_3->getPositionX() - 74, 50)); this->addChild(_skill_4); return true; }
本文出自 “风中的一粒沙” 博客,请务必保留此出处http://libinqi456.blog.51cto.com/4819343/1641220
标签:技能冷却
原文地址:http://libinqi456.blog.51cto.com/4819343/1641220