Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
The author is busy recently, so the game update is very slow, have this netizen also has been urging, but really is no way. Today 5.1 have time, again to update the game.
This article connected to the above cocos2d-x "Thunder War" (6) Intelligent enemy aircraft AI attack--flight path algorithm design and implementation (i) , or a design and implementation of the enemy's path in the game. Here the author also realizes two kinds of enemy lines. The following are the differences:
(1) The enemy aircraft flew towards the location of the hero plane.
(2) About two groups of aircraft through, in fact, a large group of aircraft from left to right and from right to left flight.
The effect of this article:
Cocos2d-x Version: 3.4
Engineering Environment: VS30213
First, the enemy aircraft flying towards the hero plane
First of all, the enemy aircraft to the location of the hero plane to fly, here is relatively simple, as long as the location of the hero plane, plus set the initial position of the enemy aircraft, then the flight path of the enemy aircraft out. The following author has drawn a picture, specifically can be seen as:
Or as follows
The position of the hero plane we can know, we can calculate the values of A and B, and can get the angle mdegree. Here the Mdegree is mainly used to rotate the enemy aircraft, if not to rotate the enemy, it does not seem to be a good effect.
Now that you know the principle, start writing code to implement it:
void gamemain::enemybuild3 (float dt) {Size winsize = director::getinstance ()->getwinsize (); Auto Spriteplane = Sprite :: Create ("air2.png");//Get elf width and height float height = spriteplane->getcontentsize (). height;float width = spriteplane-> Getcontentsize (). width;//set enemy aircraft in the upper right corner spriteplane->setposition (Vec2 (winsize.width + width/2, winsize.height + height /2)); Spriteplane->setscale (0.25); This->addchild (Spriteplane);//Calculate the angle of the line and boundary between the hero plane and the diagonal point float x = Heroplane:: GetInstance ()->getplane ()->getposition (). X;float y = heroplane::getinstance ()->getplane () GetPosition (). y;float a = winsize.width-x;float B = winsize.height-y;//radians turn angle float radians = atanf (A/b); float Mdegre E = Cc_radians_to_degrees (RADIANS); spriteplane->setrotation (180+mdegree);//Calculate the final position of the aircraft float EndX = winsize.width-( A/b) *winsize.height;float EndY = 0;//calculate flight time Float flyvelocity = 200;//run speed, you can control the pixels per second of float Flylen = sqrt ((winsize.wi DTH-ENDX) * (WINSIZE.WIDTH-ENDX) + (winsize.height-endy) * (winsize.height-enDY)); float realflyduration = flylen/flyvelocity;//actual flight time//the distance and time the bullet is running, starting from the aircraft to the bottom of the screen auto Actionmove = Moveto::create ( Realflyduration, point (EndX, EndY));//function callback after the execution of the bullet, call remove bullet function Auto Actiondone = callfuncn::create (Cc_callback_1 ( Gamemain::enemyremove, this));//continuous action sequence* sequence = Sequence::create (Actionmove, Actiondone, NULL);// The plane started running spriteplane->runaction (sequence);}
Note that the picture here is still not optimized, the enemy class has not yet written a single class, here is just a simple implementation of the next.
Then open a timer to perform this operation
Every 0.5S call once schedule (Schedule_selector (gamemain::enemybuild3), 0.5f);
Okay, now look at the results:
Enemy aircraft can be hit by the hero plane, and can change their perspective in real time, of course. This angle is counted as one.
second, about the group flying enemy aircraft
Both left and right have a row of planes, and then move left or right at the same time. The aircraft path is not a problem, the main thing is to set their starting position, so that they can all side by side. Schematic diagram is as follows:
The place to note here is to set the position of the enemy aircraft, remember to add the enemy image offset, COCO2DX set the wizard position by default is the image center point as the origin, so add this offset. and the distance they move is the same, so you can use Moveby to achieve, the y-axis direction of movement for the 0,x axis direction of exercise for screen width + enemy aircraft width. Left and right enemy aircraft X axis direction of exercise is not the same. Remember! And, the Moveby action here can only be used for an enemy aircraft, if the other enemy is the same action, then you can use the Clone () function, instead of recreating a moveby action.
The overall code is as follows:
void Gamemain::enemybuild4 (float dt) {Size winsize = director::getinstance ()->getwinsize (); Point origin = Director::getinstance ()->getvisibleorigin ()//Generate left enemy auto SpritePlane1 = Sprite::create ("Air5.png") Auto SpritePlane2 = sprite::create ("Air5.png"), Auto SpritePlane3 = Sprite::create ("Air5.png");//Generate Edge Enemy Auto SpritePlane4 = Sprite::create ("Air5.png"), Auto SpritePlane5 = Sprite::create ("air5.png"); auto SpritePlane6 = Sprite:: Create ("Air5.png");//rotation angle spriteplane1->setrotation (; spriteplane2->setrotation);spriteplane3-> Setrotation (+); spriteplane4->setrotation ( -90); Spriteplane5->setrotation ( -90);spriteplane6-> Setrotation (-90);//Set Zoom//spriteplane1->setscale (0.3);//spriteplane2->setscale (0.3); Spriteplane3->setscale (0.3);//Get elf width and height float height = spriteplane1->getcontentsize (). height;float width = Spriteplane1->getcontentsize (). width; Place enemy aircraft position spriteplane1->setposition (VEC2 (-WIDTH/2, winsize.height-height/2-10)); Spriteplane2->setpoSition (VEC2 (-WIDTH/2, Spriteplane1->getposition (). Y-2 * height-10)); Spriteplane3->setposition (VEC2 (-WIDTH/2 , Spriteplane2->getposition (). Y-2 * height-10)) spriteplane4->setposition (VEC2 (winsize.width + WIDTH/2, Sprite Plane1->getposition (). y-height-10); Spriteplane5->setposition (VEC2 (winsize.width + WIDTH/2, spritePlane4- >getposition (). Y-2 * height-10)) spriteplane6->setposition (VEC2 (winsize.width + WIDTH/2, Spriteplane5->get Position (). Y-2 * height-10));//Add Sprite This->addchild (SPRITEPLANE1) to the layer; This->addchild (spritePlane2);this-> AddChild (SpritePlane3); This->addchild (spritePlane4); This->addchild (spritePlane5);//Calculate Flight time float Flyvelocity = 200;//running speed, can control itself, the pixels per second float Flylen = winsize.width+width;float realflyduration = flylen/flyvelocity;/ /Actual flight time//the distance and time of the bullet to run from the aircraft to the bottom of the screen auto actionMove1 = Moveby::create (realflyduration, point (flylen,0)); auto ActionMove2 = Moveby::create (realflyduration, point (-flylen, 0))//After the execution of the bulletfunction callback, invoke remove bullet function Auto Actiondone = callfuncn::create (Cc_callback_1 (Gamemain::enemyremove, this));//Continuous action sequence* Sequence1 = Sequence::create (actionMove1, Actiondone, NULL); sequence* Sequence2 = Sequence::create (Actionmove1->clone (), Actiondone, NULL); sequence* sequence3 = Sequence::create (Actionmove1->clone (), Actiondone, NULL); sequence* Sequence4 = sequence::create (ActionMove2, Actiondone, NULL); sequence* Sequence5 = Sequence::create (Actionmove2->clone (), Actiondone, NULL);//The plane starts running spriteplane1-> Runaction (Sequence1); spriteplane2->runaction (SEQUENCE2); spriteplane3->runaction (sequence3); spriteplane4- >runaction (Sequence4); spriteplane5->runaction (SEQUENCE5);}
Then the same principle, open a timer, to see the effect:
Every 0.5 calls once schedule (Schedule_selector (GAMEMAIN::ENEMYBUILD4), 0.5f);
Two types of aircraft come together:
Iii. Summary
Here the path of the aircraft design four, because it has not been optimized, so memory consumption will be a bit more, the later I will be the enemy class all put in a plist, this way memory will be small point. In fact, in the flying game. There is also a boss machine, the boss machine intelligent AI Design is also a very fun. Of course, the enemy bullets are also very important, and this part of the content will be placed in the design of enemy aircraft after the completion of the. In the next lecture, we will encapsulate our own enemy aircraft class.
Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
Cocos2d-x "Thunder War" (6) Intelligent enemy aircraft AI attack--flight path algorithm design and implementation (II.)