• cocos2dx技能冷却


    今天看了一个博客 技能冷却,内部原理 没有看懂,但在他的基础上 加了一个代理方法,使外部类能够捕获到冷却结束。博客地址http://http://blog.csdn.net/z104207/article/details/8059098 无侵权意思,
    
    效果:
    
      
    首先 建了一个SkillButton的类
    .h文件
    ifndef _SKILLBUTTON_H_
    
    #define _SKILLBUTTON_H_
    
    #include "cocos2d.h"
    
    class CMTouchSpriteProtocol;
    
    class SkillButton : public cocos2d::CCNode
    
    {
    
    public:
    
        SkillButton();
    
        virtual ~SkillButton();
    
        
    
       
    
        static SkillButton* createSkillButton(float cdTime,
    
                                              const char* stencil_file_name,
    
                                              const char* button_normal_name,
    
                                              const char* button_click_name);
    
        
    
       
    
        void    setCDTime(float time) { mCDTime = time; }
    
        float   getCDTime() const { return mCDTime; }
    
        
    
       
    
        void    skillClickCallBack(cocos2d::CCObject* obj);
    
        
    
       
    
        void    skillCoolDownCallBack(cocos2d::CCNode* node);
    
         CC_PROPERTY(CMTouchSpriteProtocol*, _touchSpriteProtocol, TouchSpriteProtocol);
    
        
    
    private:
    
        
    
       
    
        bool    init(float cdTime, const char* stencil_file_name,const char* button_normal_name, const char* button_click_name);
    
        
    
    private:
    
        cocos2d::CCMenuItemImage*   mItemSkill;     // 技能按钮
    
        cocos2d::CCMenu*            mMenuSkill;     // 技能按钮所在menu
    
        cocos2d::CCSprite*          mStencil;       // 蒙板精灵,黑色半透明(这个是为了显示一个冷却计时效果)
    
        cocos2d::CCProgressTimer*   mProgressTimer; // 时间进度条精灵(360度旋转)
    
        float                       mCDTime;          // CD时间
    
    };
    
    class CMTouchSpriteProtocol {
    
    public:
    
        // optional
    
        virtual void CMTouchSpriteSelectSprite(SkillButton*sprite) {
    
        };
    
        
    
    };
    
    #endif
    
    .m文件
    
     
    
    #include "SkillButton.h"
    
    USING_NS_CC;
    
     
    
    SkillButton::SkillButton():
    
    mItemSkill(NULL),
    
    mMenuSkill(NULL),
    
    mStencil(NULL),
    
    mProgressTimer(NULL),
    
    mCDTime(1.f)
    
    {
    
        
    
    }
    
     
    
    SkillButton::~SkillButton()
    
    {
    
        
    
    }
    
     
    
    SkillButton* SkillButton::createSkillButton(float cdTime, const char* stencil_file_name, constchar* button_normal_name, const char* button_click_name)
    
    {
    
        SkillButton* skillButton = new SkillButton();
    
        if (skillButton && skillButton->init(cdTime, stencil_file_name, button_normal_name, button_click_name))
    
        {
    
            skillButton->autorelease();
    
            return skillButton;
    
        }
    
        else
    
        {
    
            delete skillButton;
    
            skillButton = NULL;
    
        }
    
        
    
        return NULL;
    
    }
    
     
    
    bool SkillButton::init(float cdTime, const char* stencil_file_name, const char* button_normal_name, const char* button_click_name)
    
    {
    
        CCAssert(stencil_file_name, "SkillButton::init stencil_file_name != NULL");
    
        CCAssert(button_normal_name, "SkillButton::init button_normal_name != NULL");
    
        CCAssert(button_click_name, "SkillButton::init button_click_name != NULL");
    
        
    
        // Notice:添加child时要注意上下层
    
        // 最下方是CCMenuItemImage 其次是模版图片 最上方是CCProgressTimer
    
        
    
        // 添加技能按钮
    
        mItemSkill = CCMenuItemImage::create(button_normal_name, button_click_name, this,menu_selector(SkillButton::skillClickCallBack));
    
        mItemSkill->setPosition(CCPointZero);
    
        
    
        mMenuSkill = CCMenu::create(mItemSkill, NULL);
    
        mMenuSkill->setPosition(CCPointZero);
    
        addChild(mMenuSkill, -100);
    
        
    
        // 添加阴影模版
    
        mStencil = CCSprite::create(stencil_file_name);
    
        mStencil->setPosition(CCPointZero);
    
        mStencil->setVisible(false);
    
        addChild(mStencil);
    
        
    
        // 添加旋转进度条精灵
    
        CCSprite* progressSprite = CCSprite::create(button_normal_name);
    
        mProgressTimer = CCProgressTimer::create(progressSprite);
    
        mProgressTimer->setPosition(CCPointZero);
    
        mProgressTimer->setVisible(false);
    
        addChild(mProgressTimer, 100);
    
        
    
        mCDTime = cdTime;
    
        return true;
    
    }
    
     
    
     
    
    void SkillButton::skillClickCallBack(cocos2d::CCObject* obj)
    
    {
    
        // 冷却计时,即时状态技能按钮不可点击
    
        mItemSkill->setEnabled(false);
    
        
    
        // 模版可见
    
        mStencil->setVisible(true);
    
        
    
        // 设置精灵进度条为顺时针
    
        mProgressTimer->setVisible(true);
    
        mProgressTimer->setType(kCCProgressTimerTypeRadial);
    
        
    
        //准备一个5秒旋转360度的动画(逐渐覆盖半透模板形成冷却效果;这里进行计时冷却动画的实现和时间控制)
    
        CCActionInterval* action_progress_to = CCProgressTo::create(20, 100);
    
        CCCallFunc* action_callback = CCCallFuncN::create(this,callfuncN_selector(SkillButton::skillCoolDownCallBack));
    
        mProgressTimer->runAction(CCSequence::create(action_progress_to, action_callback, NULL));
    
    }
    
     
    
     
    
    void SkillButton::skillCoolDownCallBack(CCNode* node)
    
    {
    
        // 设置蒙板不可见
    
        mStencil->setVisible(false);
    
        
    
        // 进度条技能不可见
    
        mProgressTimer->setVisible(false);
    
        
    
        // 按钮置为可用
    
        mItemSkill->setEnabled(true);
    
        _touchSpriteProtocol->CMTouchSpriteSelectSprite(this);
    
    }
    
    // 设置协议
    
    void SkillButton::setTouchSpriteProtocol(CMTouchSpriteProtocol *protocol) {
    
        _touchSpriteProtocol = protocol;
    
    }
    
    CMTouchSpriteProtocol*SkillButton::getTouchSpriteProtocol() {
    
        return _touchSpriteProtocol;
    
    }
    
     
     
    在 HelloWorldScene调用
    #include "cocos2d.h"
    
    #include "SkillButton.h"
    
    class HelloWorld : public cocos2d::CCLayer,publicCMTouchSpriteProtocol
    
    {
    
    public:
    
        virtual bool init();  
    
        static cocos2d::CCScene* scene();
    
        SkillButton*mSkillButton;
    
        void CMTouchSpriteSelectSprite(SkillButton *sprite);//重写代理方法
    
        CREATE_FUNC(HelloWorld);
    
     
    
    };
    
     
    
    #endif
    
     
    
     
    
    mSkillButton = SkillButton::createSkillButton(2.f, "stencil.png", "click.png","normal.png");
    
        mSkillButton->setPosition(ccp(400/2, 400/2));
    
        addChild(mSkillButton);
    
        mSkillButton->setTouchSpriteProtocol(this);
    
    重写代理方法void HelloWorld::CMTouchSpriteSelectSprite(SkillButton*button){
    
    //做冷却结束的事件
    
    }
     
  • 相关阅读:
    SSL证书的生成方法
    某些系统文件破坏后的修复方式
    dd命令的使用简介
    浅谈访问控制列表(ACL)
    发送端usleep(900)单线程带宽
    1024个读出线程的测试结果
    寻找ROS取数的瓶颈:思路整理(2)
    ROS:32个接收进程的接收带宽
    ROS: 将接收端的接收函数注释掉,TCP只发送,不接受数据时的recv-Q和send-Q长度实验
    ROS:16个接收进程的接收带宽
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3019891.html
Copyright © 2020-2023  润新知