Study of Cocos2d-X in the Development of deluxe edition aircraft vs. game series
Zookeeper
After introducing the previous articles, we learned the implementation of the main character class in the previous article. 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 king, 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, clear
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:
12345678910111213141516171819 |
# Include "cocos2d. h" USING_NS_CC; class BulletSprite: publiccocos2d: Layer {public: BulletSprite ();~ BulletSprite (); virtualbool init (); CREATE_FUNC (BulletSprite); Animation * f_createAnimate (intcount, intfps); // create a bullet running Animation voidremoveBullet (Node * pNode ); // remove a bullet that is beyond the visible range of the screen or clear voidShootBullet (floatdt) after the bullet collision; // launch the bullet, rendering the bullet and the bullet's flight action public: Vector VecBullet; // The Bullet container SpriteBatchNode * bulletBatchNode; // batch rendering node }; |
The functions are described in detail and not explained too much. The following describes the content of BulletSprite2.cpp:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
# Include "BulletSprite. h" # include "PlaneLayer. h" BulletSprite: BulletSprite () {} BulletSprite ::~ BulletSprite () {} boolBulletSprite: init () {boolbRet = 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); returnbRet;} voidBulletSprite: ShootBullet (floatdt) {Size winSize = Director: getInstance ()-> getWinSize (); autoPlanePos = PlaneLayer:: sharedPlane-> getChildByTag (AIRPLANE)-> getPosition (); // create a bullet autospritebullet = Sprite: createWithSpriteFrameName ("bullet_1.png") from the cache "); // 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); floatflyLen = winSize. height-PlanePos. y; floatflyVelocity = 320/1; // The running speed, which can be controlled by the user. The pixel floatrealFlyDuration = flyLen/flyVelocity is used per second; // the actual flight time // The distance and time of the bullet run, auto actionMove = MoveTo: create (realFlyDuration, Point (bulletPos. x, winSize. height); // call the function callback after the bullet is executed, and call the autoactionDone = CallFuncN: create (CC_CALLBACK_1 (BulletSprite: removeBullet, this) function to remove the bullet )); sequence * sequence = Sequence: create (actionMove, actionDone, NULL); spritebullet-> runAction (sequence);}/*** remove a bullet from the container, also remove */voidBulletSprite: 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 (intcount, int fps) {charbuff [16]; Animation * panimation = Animation: create (); panimation-> setDelayPerUnit (1.0/fps); for (intid = 1; id <= count; id ++) {sprintf (buff, "bullet_0000d.png", id); panimation-> addSpriteFrame (SpriteFrameCache: getInstance () -> getSpriteFrameByName (buff);} returnpanimation ;} |
Notes in surface functions are more detailed. The key is to pay attention to the use of batch rendering mechanisms, which is a major improvement in 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.You are welcome to study and make progress together.