This is Evankaka 's blog, you are welcome to discuss and Exchange ~~~~~~
Reprint Please specify the source http://blog.csdn.net/evankaka/article/details/42747719
This article will be mainly to achieve the game development process, the ability to cool the implementation of a button point once, there will be a cooling time, after the cooling time is over, you can click again, generally this is to achieve special skills. Here, I also wrote a class, very convenient, as long as the class added to your project, add two pictures, you can achieve the function.
Cocos2d-x version: 2.2.5
Engineering Environment: WINDOWS7+VS2010
Open by: Put the project under the project folder in the Cocos2d-x installation directory with VS Open
The effect of this article:
Directory
First, the ability to cool the custom class
Ii. Methods of Use
Third, the effect
First, the ability to cool the custom class
Here the skills to cool, in fact, the core is two ccsprite, and then a ccprogresstimer to achieve, the following is the implementation of a code, you can change the picture, you can see the effect
<span style= "FONT-SIZE:18PX;" >ccsprite *s=ccsprite::create ("Skill_back.png");//skill_back.png is a lighter picture Ccprogresstimer *pt= Ccprogresstimer::create (s);p t->setposition (CCP (200,200)); The spinning CD realizes Pt->settype (Cocos2d::ccprogresstimertype (kccprogresstimertyperadial)); The appearance of //pt->settype (Cocos2d::ccprogresstimertype (Kccprogresstimertypebar)) from the middle to the outside; This->addchild (pt,4); Ccprogressto *t=ccprogressto::create (8,100);p t->runaction (Ccrepeatforever::create (t)); </span>
The above code is definitely not used directly, why, because it is an infinite playing process, and we want to make touch to the button when it is only skill cooled. So here, I can encapsulate a skill-cooled class,
The specific principle is actually to add touch events to the Genie, which is very critical!
Below, look at the design of the code header file SkillButton.h
<pre name= "code" class= "CPP" ><span style= "font-size:18px;" > #include "ControlButton.h" #include "cocos2d.h" Using_ns_cc;class skillbutton:public cocos2d::cclayer{public:// Creating a cooling skill button, create and Init are linked together, calling create will inevitably call Initstatic skillbutton* Create (const char* Fore_name,const char* Back_ name);//Initialize button BOOL Init (const char* fore_name,const char* back_name);//start cooling skill void beginskill ();//void Endskill after cooling skill ( );//Determine if the cooldown skill bool isskilling;//begins to touch virtual bool Cctouchbegan (Cctouch *ptouch, ccevent *pevent); Touch ends virtual void cctouchended (Cctouch *ptouch, ccevent *pevent); Registered Touch agent support void Registerwithtouchdispatcher (); Private:ccsprite *fore;//fore is a lighter picture ccsprite *back;//back is a darker picture ccprogresstimer *pt;//Skill effect};</span>
then the implementation file SkillButton.cpp
#include "FlyWord.h" flyword* flyword::create (const char *word,const int fontsize,ccpoint begin) {flyword* ret = new Flywor D ();//It is safer to write some if (ret && ret->init (word,fontsize,begin)) {ret->autorelease (); return ret;} Cc_safe_delete (ret);//Safely remove return nullptr;} BOOL Flyword::init (const char *word,const int fontsize,ccpoint begin) {if (! Ccnode::init ()) {return false;} Initialize _begin = Begin;m_plabel = Cclabelttf::create (Word, "Marker Felt", fontSize);//Set color cccolor3b RGB; rgb.r=255; rgb.g=0; Rgb.b=0;m_plabel->setcolor (RGB); This->addchild (M_plabel); This->setposition (CCP (BEGIN.X,BEGIN.Y));// After the initialization is complete, the word flying () is started, and return is true; The text is floated from bottom to top void flyword::flying () {ccmoveby* moveact=ccmoveby::create (0.5f,ccpointmake (0,70));//0.5 seconds Move up 70//create callback action , cccallfunc* callfunc=cccallfunc::create (This,callfunc_selector (flyword::flyend)) after the text has been streamed out;// Create a continuous action ccactioninterval* act=ccsequence::create (Moveact,callfunc,null);//setvisible (true); This->runaction (act );} Delete all objects after text fluttering void Flyword::flyend () {//After completion remove this object from memory->removeallchildrenwithcleanup (True); This->removefromparentandcleanup (true);}
It must be noted here to add Registerwithtouchdispatcher (), and Ccdirector::shareddirector ()->gettouchdispatcher () Addtargeteddelegate (this,0,false); the function J inside parameter is set to False, if True indicates that only the current layer can intercept the touch event.
Ii. Methods of Use
Add header File # # "SkillButton.h" where you want to use the HelloWorldScene.h
To define a member variable:
private:hrocker* rocker;//joystick hero* hero;///Wizard controlbutton* btn;//Button control variable map* mymap;//map Progressview * M_pprogressview; Blood bar Monster *monster1;//monster kind 1 skillbutton* btn_skill;//skill button
Then add it in the implement Init () function.
Add Skill button btn_skill=skillbutton::create ("Skill_back.png", "skill_fore.png"); Btn_skill->setposition (CCP ( visiblesize.width-150,60)); This->addchild (btn_skill,2);
Here are two pictures of
And
In addition, I changed the image of the attack button
Third, the effect
Let's take a look at the effect
How long does it take to cocos2d-x skill cooling? ---game development "Zhao Yun to Combat" (9)