Cocos2d-x lightning war (3)-unlimited bullet launch, cocos2d

Source: Internet
Author: User

Cocos2d-x lightning war (3)-unlimited bullet launch, cocos2d

Original works of Lin bingwen Evankaka. Reprinted please indicate the source

In this article, we want to implement a lightning game. At the beginning of the game, a hero plane can launch bullets infinitely. The idea here is to create a layer for a bullet. There is no timer on this layer. At every time, a bullet is generated based on the position introduced by the current hero aircraft, set the bullet moving event and the moving event (that is, delete the bullet and save memory ).

Final effect:

Cocos2d-x version: 3.4

Project Environment: VS30213

I. Hero Bullet Layer

1. HeroBulletLayer. h

/*** Function create bullet and initialize bullet movement * by Lin bingwen ( blog: * time 2015.3.14 */# include "cocos2d. h "USING_NS_CC; const float FlYVElOCITY = 500; // run speed, which can be controlled by yourself. pixel class HeroBulletLayer: public cocos2d: Layer {public: heroBulletLayer (Node * heroPlane );~ HeroBulletLayer (); virtual bool init (); // create a static HeroBulletLayer * create (Node * heroPlane) based on the hero plane ); // remove a bullet beyond the visible range of the screen or clear the void removeBullet (Node * pNode) after the bullet collision; // launch the bullet to render the bullet and fly the bullet, the default value is a single bullet void ShootBullet (float dt); // returns the bullet list Vector <Sprite *> & GetBullet (); public: Vector <Sprite *> vecBullet; // The Bullet container SpriteBatchNode * bulletBatchNode; // batch rendering Node * heroPlane; // imported hero aircraft };

2. HeroBulletLayer. cpp

/*** Function create bullet and initialize the bullet movement * by Lin bingwen ( blog: * time 2015.3.14 */# include "HeroBulletLayer. h "HeroBulletLayer: HeroBulletLayer (Node * heroPlane) {this-> heroPlane = heroPlane;} HeroBulletLayer ::~ HeroBulletLayer () {}/ *** static method for creating bullets * @ param heroPlane is a hero airplane */HeroBulletLayer * HeroBulletLayer: create (Node * heroPlane) {HeroBulletLayer * pRet = new HeroBulletLayer (heroPlane); if (pRet & pRet-> init () {pRet-> autorelease (); return pRet;} else {delete pRet; pRet = NULL; return NULL ;}} bool HeroBulletLayer: init () {bool bRet = false; do {CC_BREAK_IF (! Layer: init (); // create a BatchNode node bulletBatchNode = SpriteBatchNode: create ("bullet1.png"); this-> addChild (bulletBatchNode ); // call the trigger function this-> schedule (schedule_selector (HeroBulletLayer: ShootBullet), 0.2f); bRet = true;} while (0); return bRet ;} /*** use the cached method to create a bullet and initialize the bullet movement and post-Motion Events */void HeroBulletLayer: ShootBullet (float dt) {Size winSize = Director :: getInstance ()-> getWinSize (); auto PlanePos = heroPlane-> getPosition (); // create a bullet auto spritebullet = Sprite :: createWithTexture (bulletBatchNode-> getTexture (); // Add the created bullet to BatchNode for batch rendering bulletBatchNode-> addChild (spritebullet ); // Add the created bullet to the vecBullet container. pushBack (spritebullet); Point bulletPos = (Point (PlanePos. x, PlanePos. y + heroPlane-> getContentSize (). height/2 + 20); spritebullet-> setPosition (bulletPos); spritebullet-> setScale (0.8f); float flyLen = winSize. height-PlanePos. y; float realFlyDuration = flyLen/FlYVElOCITY; // actual flight time // The distance and time of bullet run, 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 (HeroBulletLayer: removeBullet, this) to remove the bullet )); // The Bullet starts to run Sequence * sequence = Sequence: create (actionMove, actionDone, NULL); spritebullet-> runAction (sequence);}/*** remove the bullet, remove a bullet from the container and also remove */void HeroBulletLayer: removeBullet (Node * pNode) {if (NULL = pNode) {return;} from SpriteBatchNode ;} sprite * bullet = (Sprite *) pNode; this-> bulletBatchNode-> removeChild (bullet, true); vecBullet. eraseObject (bullet);}/*** returns the bullet list for Collision Detection with the enemy */Vector <Sprite *> & HeroBulletLayer: GetBullet () {return vecBullet ;}



// Create BatchNode node bulletBatchNode = SpriteBatchNode: create ("bullet1.png ");
The method of adding the bullet image to the cache is used here.

// Create the bullet auto spritebullet = Sprite: createWithTexture (bulletBatchNode-> getTexture () from the cache ());
If you do not do this, the game will consume a lot of memory and it will be very slow!

2. Here, the create method is rewritten to include a parameter and use it to pass in the hero airplane.

Ii. Usage

// Add the bullet HeroBulletLayer * mHeroBulletLayer = HeroBulletLayer: create (mHeroPlane); this-> addChild (mHeroBulletLayer, 1 );


Please note that mHeroPlane is the hero aircraft following the finger moving in the last lecture, see the Cocos2d-x here "lightning war" (2)-The genie with the finger moving, where do you want me to go!

Iii. Ideas

1. The above Hero Bullet class is very useful. As long as you pass in the position of a hero plane, it will generate the Hero Bullet layer and constantly call the timer to generate bullets, added to the current layer and Vector at the same time (used to save all bullets)

2. Calculate the moving distance of the bullet based on the current location of the hero aircraft, and then the speed is set by yourself. Then, you can calculate the moving distance of the bullet in a straight line.

3. When the bullet moves out of the field of view, it will be deleted and deleted in the vector.

4. GetBullet (); is used to obtain the current vector bullet set, and then we need to extract it one by one to determine whether it is in conflict with the enemy. If yes, call removeBullet (Node * pNode. For example:

void GameMain::update(float dt){auto *mEnemyPlane = getChildByTag(200);Vector <Sprite *> mVecHeroBullet = mHeroBulletLayer->GetBullet();for (int i = 0; i<mVecHeroBullet.size();i++){if (mEnemyPlane->boundingBox().intersectsRect(>boundingBox())){mHeroBulletLayer->removeBullet(;}}}

The effect is as follows: Pay attention to the traversal usage of the vector here. It cannot be []. cocos2dx does not reload this. Use.

Here is just an example of how to use it. After the bullet hits the plane, it will be deleted, and the enemy plane has not processed it.

Original works of Lin bingwen Evankaka. Reprinted please indicate the source

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: 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.