How long will Cocos2d-x skill cool? --- Game development: Zhao Yun fight (9), cocos2d
Here isEvankakaWelcome to the discussion and exchange ~~~~~~
Reprinted please indicate the source http://blog.csdn.net/evankaka/article/details/42747719
This article will focus on the implementation of skill cool-down during game development. Once a button is clicked, there will be a cool-down time. After the cool-down time expires, you can click it again, this is generally a special skill. Here, I also wrote a class, which is very convenient. You only need to add this class to your project and add two images to implement the function.
Cocos2d-x version: 2.2.5
Project Environment: Windows 7 + VS2010
Open Method: place the project in the project folder under the cocos2d-x installation directory open with
Effect:
Directory
I. Skill cooling Customization
Ii. Usage
Iii. Effect
I. Skill cooling Customization
Here the skill cool-down is actually the core is two CCsprite, and then a CCProgressTimer to achieve it. Below is a code for implementation. You can change the picture to see the effect.
<Span style = "font-size: 18px;"> CCSprite * s = CCSprite: create ("skill_back.png"); // skill_back.png is the highlighted image CCProgressTimer * pt = CCProgressTimer:: create (s); pt-> setPosition (ccp (200,200); // compress the CD to implement pt-> setType (cocos2d: CCProgressTimerType (kCCProgressTimerTypeRadial )); // appears from the middle to the outside // pt-> setType (cocos2d: CCProgressTimerType (kCCProgressTimerTypeBar); this-> addChild (pt, 4); CCProgressTo * t = CCProgressTo:: create (8,100); pt-> runAction (CCRepeatForever: create (t); </span>
The above Code cannot be used directly. Why? Because it is an infinite playback process, and we need to let it cool when we touch the button. So here, I can encapsulate a skill cooling class,
The specific principle is to add touch events to the Genie. This is critical!
Next, let's take a look at the designed 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: // create cool-down skill button. create and init are connected together, when you call create, you must call initstatic SkillButton * create (const char * fore_name, const char * back_name); // The initialization button bool init (const char * fore_name, const char * back_name); // start the cool-down skill void BeginSkill (); // void EndSkill () after the cool-down skill ends; // determine whether the cool-down skill bool IsSkilling; // start to touch virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); // touch and end virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ); /// registered touch proxy supports void registerWithTouchDispatcher (); private: CCSprite * fore; // fore is a bright image CCSprite * back; // back is a dark image CCProgressTimer * pt; // skill effect}; </span>
Then implement the SkillButton. cpp file.
# Include "FlyWord. h "FlyWord * FlyWord: create (const char * word, const int fontSize, CCPoint begin) {FlyWord * ret = new FlyWord (); // write more securely if (ret & ret-> init (word, fontSize, begin) {ret-> autorelease (); return ret ;} CC_SAFE_DELETE (ret); // safely delete 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 the color ccColor3B RGB and 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 Initialization is complete, the Flying (); return true;} // text starts to float from bottom to top void FlyWord: Flying () {CCMoveBy * moveact = CCMoveBy: create (0.5f, CCPointMake (0.5); // seconds to move up 70 // create a callback action, CCCallFunc * callFunc = CCCallFunc: create (this, callfunc_selector (FlyWord: Flyend); // create a continuous action CCActionInterval * act = CCSequence: create (moveact, callFunc, NULL); // setVisible (true); this-> runAction (act);} // delete all objects void FlyWord: Flyend () after text fluttering () {// Delete this object from the memory after completion. this-> removeAllChildrenWithCleanup (true); this-> removeFromParentAndCleanup (true );}
You must add registerWithTouchDispatcher () Here, and set CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, false ); in Function j, the parameter is set to false. If it is set to true, only the current layer can capture touch events.
Ii. Usage
Add the header file # include "SkillButton. h" to HelloWorldScene. h"
Define member variables:
Private: HRocker * rocker; // joystick Hero * hero; // genie ControlButton * btn; // Map * mymap; // Map ProgressView * m_pProgressView; // Monster * monster1; // Monster type 1 SkillButton * btn_skill; // skill button
Then add
// 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 );The two images here are
And
In addition, I changed an attack button image.
Iii. Effect
Let's take a look at the effect.