Cocos2d-x blood bar follow the monster movement ---- game development "zhao Yun to fight" (5), monster bullet ball Zhao Yun

Source: Internet
Author: User

Cocos2d-x blood bar follow the monster movement ---- game development "zhao Yun to fight" (5), monster bullet ball Zhao Yun

This chapter on the basis of the previous Cocos2d-x custom blood bar and Its beautification ---- game development "Zhao cloud to fight" (4)

Design a monster category and display a blood bar above the monster. The blood bar follows the movements of the monster. In the previous lecture, we usually use a blood bar on top of the head in our games. Here we want to implement this function.


Important: because the trial period of TexturePacker is over and cannot be reused, the following animations are not merged with plist and the whole PNG. At the same time, Zhao Yun's images were not very animated, so he switched to Zhao Yun's image. In addition, the classes of the entire project are archived separately, which makes it easier to understand. Therefore, some functions of hero. h and hero. cpp have been changed, and the called areas have also been changed. Be sure to pay attention here!


This is an effect of this chapter:


(Effect of the next chapter)


1. Change hero categories

Zhao Yun's image: the animation is implemented by reading a PNG Image (without merging Plist and the whole PNG)


The changed Hero class Hero. h (the class functions and variables remain unchanged, and the usage method remains unchanged. Only the SetAnimation function parameter is changed)

# Ifndef _ HERO_H __# define _ HERO_H __# include "cocos2d. h" using namespace cocos2d; class Hero: public cocos2d: CCNode {public: Hero (void );~ Hero (void); // create the Hero void InitHeroSprite (char * hero_name) based on the image name; // set the animation. num indicates the number of images, run_directon indicates the sprite face orientation, and false indicates the right direction, name_each is the public name part of each small image in name_png void SetAnimation (const char * name_each, const unsigned int num, bool run_directon); // stop the animation void StopAnimation (); // attack animation void AttackAnimation (const char * name_each, const unsigned int num, bool run_directon); // after the attack animation ends void AttackEnd (); // determine whether the hero has moved to the middle of the window. visibleSize is the size of the current window, bool JudgePositona (CCSize visibleSize); // determine whether the animation bool IsRunning is running; // determine whether the animation bool IsAttack is under attack; // the direction of the Hero movement is bool HeroDirecton; CREATE_FUNC (Hero); private: CCSprite * m_HeroSprite; // The genie char * Hero_name; // The Name Of The sprite image used to save the initial state}; # endif // _ HERO_H __


Hero. cpp
# Include "Hero. h "USING_NS_CC; Hero: Hero (void) {IsRunning = false; // HeroDirecton = false; // Hero_name = NULL; IsAttack = false ;} hero ::~ Hero (void) {} void Hero: InitHeroSprite (char * hero_name) {Hero_name = hero_name; this-> m_HeroSprite = CCSprite: create (hero_name ); this-> addChild (m_HeroSprite);} void Hero: SetAnimation (const char * name_each, unsigned int num, bool run_directon) {if (HeroDirecton! = Run_directon) {HeroDirecton = run_directon; m_HeroSprite-> setFlipX (run_directon);} if (IsRunning) return; CCAnimation * animation = CCAnimation: create (); for (int I = 1; I <= num; I ++) {char szName [100] = {0}; sprintf (szName, "s0000d.png", name_each, I ); animation-> addSpriteFrameWithFileName (szName); // loading the animation frame} animation-> setDelayPerUnit (0.1f); animation-> setRestoreOriginalFrame (true); animation-> setLoops (-1 ); // Animated loop if (HeroDirecton! = Run_directon) {HeroDirecton = run_directon;} // wrap the animation into an action CCAnimate * act = CCAnimate: create (animation); m_HeroSprite-> runAction (act ); isRunning = true;} void Hero: StopAnimation () {if (! IsRunning) return; m_HeroSprite-> stopAllActions (); // The current genie stops all animations. // restores the original initialization texture of the Genie. this-> removeChild (m_HeroSprite, TRUE ); // Delete the original sprite m_HeroSprite = CCSprite: create (Hero_name); // restore the sprite's original texture m_HeroSprite-> setFlipX (HeroDirecton ); this-> addChild (m_HeroSprite); IsRunning = false;} void Hero: AttackAnimation (const char * name_each, const unsigned int num, bool run_directon) {if (IsAttack) return; isAttack = true; CCAnimati On * animation = CCAnimation: create (); for (int I = 1; I <= num; I ++) {char szName [100] = {0 }; sprintf (szName, "%s0000d.png", name_each, I); animation-> addSpriteFrameWithFileName (szName); // frames for loading animations} animation-> setDelayPerUnit (0.05f ); animation-> setRestoreOriginalFrame (true); animation-> setLoops (1); // animated loop if (HeroDirecton! = Run_directon) {HeroDirecton = run_directon;} // wrap the animation into an action CCAnimate * act = CCAnimate: create (animation); // create a callback action, call AttackEnd () CCCallFunc * callFunc = CCCallFunc: create (this, callfunc_selector (Hero: AttackEnd) after the attack ends; // create a continuous action CCActionInterval * attackact = CCSequence :: create (act, callFunc, NULL); m_HeroSprite-> runAction (attackact);} void Hero: AttackEnd () {// restore the original initialization texture this-> removeChild (m_HeroSprite, TR UE); // Delete the original sprite m_HeroSprite = CCSprite: create (Hero_name); // restore the sprite's original texture m_HeroSprite-> setFlipX (HeroDirecton ); this-> addChild (m_HeroSprite); IsAttack = false;} bool Hero: JudgePositona (CCSize visibleSize) {if (this-> getPositionX ()! = VisibleSize. width/2) // return false to the left of the genie; else return true; // reach the intermediate position}


Remember to change SetAnimation to the place where it is used, and change the parameter of the SetAnimation function in other places.

Void HelloWorld: update (float delta) {// determines whether to press the joystick and its type CCSize visibleSize1 = CCDirector: shareddire()-> getVisibleSize (); // get the window size switch (rocker-> rocketDirection) {case 1: hero-> SetAnimation ("hero_run", 8, rocker-> rocketRun ); if (hero-> getPositionX () <= visibleSize1.width-8) // you can change 8 to your {if (! Hero-> JudgePositona (visibleSize1) | mymap-> JudgeMap (hero, visibleSize1) // The genie can move only when they do not reach the middle of the window or the map has been moved to the edge, otherwise, only play the animation hero-> setPosition (ccp (hero-> getPosition (). x + 1, hero-> getPosition (). y); // move to the right. // The following figure shows the mobile map mymap-> MoveMap (hero, visibleSize1);} break; case 2: hero-> SetAnimation ("hero_run", 8, rocker-> rocketRun); hero-> setPosition (ccp (hero-> getPosition (). x, hero-> getPosition (). y + 1); // go up to break; case 3: hero-> SetAnimation ("hero_run", 8, rocker-> rocketRun ); if (hero-> getPositionX ()> = 8) // do not let the genie go beyond the left. You can change 8 to your favorite hero-> setPosition (ccp (hero-> getPosition (). x-1, hero-> getPosition (). y); // take break to the left; case 4: hero-> SetAnimation ("hero_run", 8, rocker-> rocketRun ); hero-> setPosition (ccp (hero-> getPosition (). x, hero-> getPosition (). y-1); // walk down break; case 0: hero-> StopAnimation (); // stop all animation and motion break ;} // determine whether to launch an attack if (btn-> isTouch) {if (hero-> IsAttack) // the hero is not attacking return; hero-> AttackAnimation ("hero_attack", 20, rocker-> rocketRun); m_pProgressView-> setCurrentProgress (m_pProgressView-> getCurrentProgress ()-10); // change the blood volume }}
Effect: Zhao Yun's images are clearer than before, and the attack images are a little smoother (20 images !)



2. Custom monsters with bloody bars

Here, the blood bar uses the previous custom blood bar. The idea is to apply the custom blood bar type ProgressView in the previous article to Monster to obtain the location of the Monster in the Monster class, then, set the location of the blood bar member variable (usually above) based on this location, and finally add the monster Genie and the blood bar class addchild ().

The following is a monster Resource: (in part, an animation is played one by one)


Monster. h:

# Ifndef _ Monster_H __# define _ Monster_H __# include "cocos2d. h "# include" ProgressView. h "USING_NS_CC; class Monster: public cocos2d: CCNode {public: Monster (void );~ Monster (void); // create a Monster Based on the image name, with no bloody void InitMonsterSprite (char * name); // void InitMonsterSprite (char * name, char * xue_back, char * xue_fore); // sets the animation. num indicates the number of images, run_directon indicates the sprite face orientation, and false indicates the right direction, name_each is the public name part of each small image in name_png void SetAnimation (const char * name_each, const unsigned int num, bool run_directon); // stop the animation void StopAnimation (); // attack animation void AttackAnimation (const char * name_each, const unsigned int num, bool run_directon); // after the attack animation ends void AttackEnd (); // return the hero CCSprite * GetSprite (); // determines whether the animation bool IsRunning is running; // checks whether the animation bool IsAttack is being attacked; // returns the direction of the hero's motion to bool MonsterDirecton; CREATE_FUNC (Monster); private: CCSprite * m_MonsterSprite; // Monster genie char * Monster_name; // The Name Of The genie image used to save the initial state: ProgressView * Monster_xue; // Monster blood bar }; # endif/_ HERO_H __


Monster. cpp:

# Include "Monster. h "USING_NS_CC; Monster: Monster (void) {IsRunning = false; // MonsterDirecton = TRUE; // Monster_name = NULL; IsAttack = false; monster_xue = NULL;} Monster ::~ Monster (void) {} CCSprite * Monster: GetSprite () {return m_MonsterSprite;} void Monster: InitMonsterSprite (char * name) {Monster_name = name; this-> m_MonsterSprite = CCSprite: create (name); m_MonsterSprite-> setFlipX (MonsterDirecton); this-> addChild (m_MonsterSprite);} void Monster: InitMonsterSprite (char * name, char * xue_back, char * xue_fore) {InitMonsterSprite (name); // set the blood bar of the monster Monster_xue = new ProgressView (); Monst Er_xue-> setPosition (ccp (m_MonsterSprite-> getPositionX () + 25, m_MonsterSprite-> getPositionY () + 50 )); // set the header on the monster // Monster_xue-> setScale (2.2f); Monster_xue-> Merge (xue_back); Monster_xue-> Merge (batch); Monster_xue-> setTotalProgress (300366f ); monster_xue-> setCurrentProgress (300366f); this-> addChild (Monster_xue);} void Monster: SetAnimation (const char * name_each, unsigned int n Um, bool run_directon) {if (MonsterDirecton! = Run_directon) {MonsterDirecton = run_directon; m_MonsterSprite-> setFlipX (run_directon);} if (IsRunning | IsAttack) return; CCAnimation * animation = CCAnimation: create (); for (int I = 1; I <= num; I ++) {char szName [100] = {0}; sprintf (szName, "s0000d.png", name_each, I ); animation-> addSpriteFrameWithFileName (szName); // frame for loading the animation} animation-> setDelayPerUnit (2.8f/14.0f); animation-> setRestoreOriginalFrame (true); anim Ation-> setLoops (-1); // animation loop // wrap the animation into an action CCAnimate * act = CCAnimate: create (animation ); m_MonsterSprite-> runAction (act); IsRunning = true;} void Monster: StopAnimation () {if (! IsRunning) return; m_MonsterSprite-> stopAllActions (); // The current genie stops all animations. // restores the original initialization texture of the Genie. this-> removeChild (m_MonsterSprite, TRUE ); // Delete the original sprite m_MonsterSprite = CCSprite: create (Monster_name); // restore the sprite's original texture m_MonsterSprite-> setFlipX (MonsterDirecton ); this-> addChild (m_MonsterSprite); IsRunning = false;} void Monster: AttackAnimation (const char * name_each, const unsigned int num, bool run_directon) {if (IsAttack | IsRunning) return; CCAnimation * animation = CCAnimation: create (); for (int I = 1; I <= num; I ++) {char szName [100] = {0 }; sprintf (szName, "%s0000d.png", name_each, I); animation-> addSpriteFrameWithFileName (szName); // frames for loading animations} animation-> setDelayPerUnit (2.8f/14.0f ); animation-> setRestoreOriginalFrame (true); animation-> setLoops (1); // One animation loop // wrap the animation into an action CCAnimate * act = CCAnimate :: create (animation); // create a callback action. After the attack ends, call AttackEnd () CCCallFunc * callFunc = CCCallFunc: create (this, callfunc_selector (Monster: AttackEnd )); // create a continuous action CCActionInterval * attackact = CCSequence: create (act, callFunc, NULL); m_MonsterSprite-> runAction (attackact); IsAttack = true;} void Monster :: attackEnd () {// restore the original initialization texture of the Genie this-> removeChild (m_MonsterSprite, TRUE); // Delete the original genie m_MonsterSprite = CCSprite: create (Monster_name ); // restore the sprite's original texture m_MonsterSprite-> setFlipX (MonsterDirecton); this-> addChild (m_MonsterSprite); IsAttack = false ;}

Usage:

HelloWorldScene. h Add the header file # include "Monster. h"

HelloWorldScene. h: Monster * monster1; // Monster type 1

Initialize the Init () function of HelloWorldScene. cpp:

This is a non-bloody monster:

// Add monster1 = Monster: create (); monster1-> InitMonsterSprite ("monster.png"); // monster1-> InitMonsterSprite ("monster.png", "xue_back.png ", "xue_fore.png"); monster1-> setPosition (ccp (visibleSize. width-150, visibleSize. height/2); this-> addChild (monster1, 1 );



This is a bloody monster:

// Add monster1 = Monster: create (); // monster1-> InitMonsterSprite ("monster.png"); monster1-> InitMonsterSprite ("monster.png", "xue_back.png ", "xue_fore.png"); monster1-> setPosition (ccp (visibleSize. width-150, visibleSize. height/2); this-> addChild (monster1, 1 );


Now, this article is over. Next we will talk about smart monsters, so that monsters can be moved and attacked!

The following is the effect of moving a blood bar following a monster (the effect of the next chapter)



Postscript:

Game development is very important for China and the United States. To find these images, they are all imported from GIF images to PS, and then exported to a PNG. The main reason is that cocos2dx does not support direct GIF playback, if that's all right (there are plug-ins on the Internet, but I have never tried it), resources and resources! It's not easy to find or use,


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.