Cocos2d-x buckle blood Floating character effects used up you will disappear -- game development "Zhao cloud to fight" (8), cocos2d
Here isEvankakaWelcome to the previous discussions and exchanges ~~~~~~
Reprinted please indicate the source http://blog.csdn.net/evankaka/article/details/42740575
One of the functions that will be implemented here is that when the volume of injured monsters decreases, a number indicating the volume of blood is deducted from the monster's center to the top of the head, and then disappears. Then there are two ways: one is to deduct the same number each time, the other is to random the number in a certain range each time, this article is followed by the blog post http://blog.csdn.net/evankaka/article/details/42689689.
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
1. Customize the FlyWord type
Ii. Usage
Iii. Summary
1. Customize the FlyWord type
In fact, here is a process of executing the CCMoveBy animation for the CCLabelTTF type.
Next let's take a look at my own definition. The header file is FlyWord. h.
# Ifndef _ FLYWORD_H __# define _ FLYWORD_H __# include "cocos2d. h "# include" cocos-ext.h "USING_NS_CC; USING_NS_CC_EXT; class FlyWord: public CCNode {public: // create text floating, create and init are connected together, when you call create, you must call initstatic FlyWord * create (const char * word, const int fontSize, CCPoint begin); // The init method accepts three parameters, which are the character strings that are float, font size, starting from where to float, the init method is mainly used to initialize FloatWord's built-in Labelbool init (const char * word, const int fontSize, CCPoint begin ); // The text float from bottom to top void Flying (); // after the text float, delete all objects void Flyend (); private: int _ fontSize; // The font size CCPoint _ begin; // CCLabelTTF * m_plabel; // font type}; # endif/_ FLYWORD_H __
Then the implementation class FlyWord. cpp
# 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);} void FlyWord: Flyend () {// Delete this object from the memory after completion. this-> removeAllChildrenWithCleanup (true); this-> removeFromParentAndCleanup (true );}
The Code has a very detailed description, and I will not describe it here
Ii. Usage
Add the header file # include "FlyWord. h" to Monster. h or Monster. cpp"
The following figure shows the amount of blood to be reduced from 10 to 30 randomly generated each time. This is also common in the game. The quantity of monsters injured each time may be different.
// Void Monster: HurtEnd () {IsHurt = false after the injury animation ends; // generate a random blood loss of 10-30 srand (UINT) GetCurrentTime ()); int x = rand () % 30 + 10; // convert it to char szName [100] = {0}; sprintf (szName, "-% d", x ); // Monster_xue-> setCurrentProgress (Monster_xue-> getCurrentProgress ()-x); // catch the blood Floating character FlyWord * wen_zi = FlyWord: create (szName, 30, CCPointMake (); // place it in the () Position of the current monster. Here () is its center. For details, see the anchor this-> addChild (wen_zi, 2); if (Monster_xue-> getCurrentProgress () = 0) {// play the animation DeadAnimation ("monster_dead", 2, MonsterDirecton );}}
The code is
// Void Monster: HurtEnd () {IsHurt = false; // generate a random blood loss of 10-30 // srand (UINT) getCurrentTime (); // int x = rand () % 30 + 10; // convert to char szName [100] = {0}; sprintf (szName, "-% d", 10); // Monster_xue-> setCurrentProgress (Monster_xue-> getCurrentProgress ()-10 ); // The FlyWord * wen_zi = FlyWord: create (szName, 30, CCPointMake (); // place it in the () Position of the current monster, here (0, 0) is its center. For details, see the anchor this-> addChild (wen_zi, 2); if (Monster_xue-> getCurrentProgress () = 0) {// play the animation DeadAnimation ("monster_dead", 2, MonsterDirecton );}}
1.10-30 random blood loss. Text from bottom to top
2. Fixed blood loss, text from bottom to top
Iii. Summary
This is mainly the design of this text class. Here, my idea is to get the hero's position, and then the CCLabelTTF is moving up from the hero's center position, when the CCMoveBy animation is executed and written as a move, it is released by itself (removeAllChildrenWithCleanup (true) and removeFromParentAndCleanup (true );). Once it is used up, the system will throw it, and wait until the monsters get rid of blood, and then use it again. This is more convenient ~ Sometimes the idea is really important during game development!