Cocos2d-x 3.0 game instance learning notes "card tower defense" Step 9-hero attack

Source: Internet
Author: User

New entry C ++ cocos2d-x3.0 tower anti-instance game

/* Description:

** 1. This game instance is the last game on the cocos2d-x game development journey, which is rewritten and noted down here with 3.0

** 2. I have also asked me about wood. He said: Write it as you like. First, do not copy the code completely. Second, You can note that it is a learning note-Good Guy.

** 3. Here With cocos2d-x 3.0 version rewriting, many places are different, but from the rewriting process also good learning cocos2d-x

*/

* ** All the code corresponding to each step and the resources used are packaged at the end.

* ** To avoid code overhead, the code in each step is marked. At first glance, the code is implemented in the first step to avoid errors and cannot be changed back (if not, git should be used again ?)

* ** For the convenience of porting to the mobile phone, Android testing is compiled for each step. Because the code can be compiled in Win32 many times, compilation will fail, the code is tested.

Note content:

1. Direct Thinking & code

2. Next Content preview

3. Download source code and resources

I. Ideas & code

If a hero attacks a monster, the update function must be used to automatically determine the target attack and go to the target to find the next target.
The first thing to solve here is that the hero must be in touch with the monsters in the current game; solve the contact problem:
Add:

Add:

//**9**get MonsterListVector<Monster*>* getMonsterList(){return &_monsterList;};
I am a lazy here and put it directly in the. H class as a public member. Note that the transfer address is
In maplayer

//**9**Vector<Monster*>* getMonsterList(){if(_monsterMgr == NULL) return NULL;return _monsterMgr->getMonsterList();};
Then, we can get the monster information in the hero's update function --> checkatkmonster.

Let's see how heroes attack monsters:
First, the hero has a current attack target _ atkmonster. At first, the constructor assigned a value of null. Then, after obtaining the monster information, the hero judges
Second, the hero needs an attack to cool down, _ isatkcooldown initial
1. If no target is attacked, the chooseaim
2. If yes, determine whether the target is dead. If it is already dead, delete it from the monster list. The target is cleared to null. The attack ends with return;
3. If a target is set and the attack cool, The ATK function will launch the attack.
4. Determine whether the target is out of the attack range
Let's take a look at the UPDATE function.

Void hero: checkatkmonster (float DT) {// ** 9 ** get the monster List Auto scene = Director: getinstance ()-> getrunningscene (); maplayer * layer = (maplayer *) scene-> getchildbytag (tag_map_layer); vector <monster *> * monsterlist = Layer-> getmonsterlist (); If (layer-> getmonsterlist () = NULL) return; If (_ atkmonster! = NULL) {If (_ atkmonster-> isdead () {monsterlist-> eraseobject (_ atkmonster); cc_safe_release_null (_ atkmonster); return ;} if (_ isatkcooldown = false) {ATK ();} // ** 9 ** check whether the target is out of the attack range if (isinatkrange (_ atkmonster-> getposition ()) = false) {cclog ("out"); cc_safe_release_null (_ atkmonster ); // clear target} else {// ** 9 ** obtain the target chooseaim (* monsterlist) from the monster list ));}}
Other functions

Void hero: chooseaim (vector <monster *> monsterlist) {for (Auto MONSTER: monsterlist) {If (monster! = NULL) {If (isinatkrange (monster-> getposition () {// ** 9 ** select the target in the range, the target cclog ("in ATK range !! "); Cc_safe_release (_ atkmonster); cc_safe_retain (monster); _ atkmonster = monster; break ;}}} bool hero: isinatkrange (point POS) {int atkrange = getatkrange (); point heropos = getposition (); float Dis = ccpdistancesq (Pos, heropos); If (DIS <= atkrange * atkrange) {return true ;} return false;} void hero: ATK () {cclog ("ATK !! "); _ Isatkcooldown = true; // the cool-down time after the attack interval reaches this-> scheduleonce (schedule_selector (hero: atkcolldownend), getatkspeed ()/1000.0f );} void hero: atkcolldownend (float DT) {_ isatkcooldown = false ;}
Finally, in the init function

//**9**this->schedule(schedule_selector(Hero::checkAtkMonster),0.3f);
After F5 debugging, add a hero. When a monster approaches, the printed attack information will be displayed.

Here, because the attack requires a bullet, and the bullet is a little bit more, leave it for the next time

The results can only be seen in the output.

Ii. Next Content preview

Allows a hero to launch a bullet and a monster to be visible after being injured --- decoration of blood records

Next time, it's almost over.

III:

----------------------------------------

Code & Resources

-------------------------------------------

Personal ignorance. Thank you for your correction and discussion.



Cocos2d-x 3.0 game instance learning notes "card tower defense" Step 9-hero attack

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.