Deluxe Edition aircraft war series (V)

Source: Internet
Author: User
Tags addchild

Cocos2d-x Ubuntu game

After introducing the previous articles, the source code will be uploaded in the next article. If you need the source code, you can see the download link in the next article. Open source is the king of the road, and sharing can grow.

This article continues to introduce the rendering of the bullet layer in the game, that is, the introduction of bulletsprite.

There are two ways to render the bullet layer. One is to directly create the genie from the cache, and then add and use the created genie directly, the second mechanism is to add the created genie to the spritebatchnode, which improves rendering efficiency. For this rendering mechanism, I will mention it here,

The common rendering mechanism is preparation, rendering, and clearing. Prepare, render, clear ....

Batch rendering mechanism: Preparation, rendering, rendering... rendering, clearing

We can see the two differences above. In batch rendering, only one preparation is cleared, which saves a lot of hardware consumption. This is an important indicator of game optimization.

I have implemented both the rendering mechanism and the two rendering mechanisms in the file. During the final operation, I still use batch rendering. We can test that the final compilation is bulletsprite2.cpp, instead of bulletsprite. cpp.

Okay, it's time to go to the code.

Let's take a look at the content of the bulletsprite. h header file:

<Span style = "font-size: 18px;"> <strong> # include "cocos2d. H "using_ns_cc; Class bulletsprite: Public cocos2d: layer {public: bulletsprite ();~ Bulletsprite (); Virtual bool Init (); create_func (bulletsprite); animation * f_createanimate (INT count, int FPS ); // create an animation void removebullet (node * pnode) for bullet running; // remove a bullet that is beyond the visible range of the screen or clear the void shootbullet (float DT) after the bullet collision ); // launch a bullet, rendering the bullet in it, and the bullet's flight action public: vector <sprite *> vecbullet; // The Bullet container spritebatchnode * bulletbatchnode; // batch rendering node }; </strong> </span>

The functions are described in detail and not explained too much. The following describes the content of bulletsprite2.cpp:

<Span style = "font-size: 18px;"> <strong> # include "bulletsprite. H "# include" planelayer. H "bulletsprite: bulletsprite () {} bulletsprite ::~ Bulletsprite () {} bool bulletsprite: Init () {bool Bret = false; do {cc_break_if (! Layer: Init (); texture2d * texture = texturecache: sharedtexturecache ()-> textureforkey ("bullet.png"); // create a batchnode bulletbatchnode = spritebatchnode :: createwithtexture (texture); this-> addchild (bulletbatchnode); // call the trigger function this-> schedule (schedule_selector (bulletsprite: shootbullet), 0.2f) every seconds ); bret = true;} while (0); Return Bret;} void bulletsprite: shootbullet (float DT) {size winsize = Director: getinstance ()-> getwinsize (); auto planepos = planelayer: sharedplane-> getchildbytag (airplane)-> getposition (); // create a bullet from the cache auto spritebullet = sprite: Fill ("bullet_1.png "); // Add the created bullet to batchnode for batch rendering bulletbatchnode-> addchild (spritebullet); // Add the created bullet to the container vecbullet. pushback (spritebullet); point bulletpos = (point (planepos. x, planepos. Y + planelayer: sharedplane-> getchildbytag (airplane)-> getcontentsize (). height/2 + 20); spritebullet-> setposition (bulletpos); spritebullet-> setscale (0.4f); float flylen = winsize. height-planepos. y; float flyvelocity = 320/1; // The running speed, which can be controlled by the user. The pixel float realflyduration = flylen/flyvelocity that is used every second; // actual flight time // The distance and time of bullet running, starting from the plane to the top of the screen auto actionmove = moveTo: Create (realflyduration, point (bulletpos. x, winsize. height); // call the function callback after the bullet completes the action and call the function auto actiondone = callfuncn: Create (cc_callback_1 (bulletsprite: removebullet, this) to remove the bullet )); sequence * sequence = sequence: Create (actionmove, actiondone, null); spritebullet-> runaction (sequence);}/*** remove a bullet from the container, also remove */void bulletsprite: removebullet (node * pnode) {If (null = pnode) {return;} sprite * bullet = (sprite *) pnode from spritebatchnode; this-> bulletbatchnode-> removechild (bullet, true); vecbullet. eraseobject (bullet);}/*** obtain the bullet animation image from the cache and create the bullet animation. The created animation */animation * bulletsprite: f_createanimate (INT count, int FPS) {char buff [16]; animation * panimation = Animation: Create (); panimation-> setdelayperunit (1.0/FPs); For (INT id = 1; ID <= count; Id ++) {sprintf (buff, "bullet_0000d.png", ID); panimation-> addspriteframe (spriteframecache: getinstance () -> getspriteframebyname (buff);} return panimation;} </strong> </span>
The comments in the above functions are more detailed. The key is to pay attention to the use of batch rendering mechanisms, which is a major improvement of game optimization.

I have introduced so many major logic layers of the game that I have not yet begun to introduce. I can't wait. In the next article, I will upload the game source code. If you need it, please refer to the next article.

This article introduces the implementation of the main logic layer of the game.


Deluxe Edition aircraft war series (V)

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.