Deluxe Edition aircraft vs series (III) and deluxe edition aircraft vs Series

Source: Internet
Author: User

Deluxe Edition aircraft vs series (III) and deluxe edition aircraft vs Series

This article describes the enemy classes in the game, including the implementation of the enemy classes in EnemySprite. h and EnemySprite. cpp.

There are four main types of enemies in the game. Three different enemy hosts and bombs are randomly stored on them, and each enemy has its own life value, when the life value is 0, it will be removed from the game.

Check the header file of the enemy information: EnemyInfo. h.

#pragma onceconst int ENEMY1_MAXLIFE=3;const int ENEMY2_MAXLIFE=5;const int ENEMY3_MAXLIFE=10;const int ENEMY1_SCORE=1;const int ENEMY2_SCORE=6;const int ENEMY3_SCORE=30;enum EnemyType{Enemy1 = 1,Enemy2,Enemy3,Enemy4,};
This file mainly sets the enumeration type to distinguish different enemies. At the same time, it sets the number of human lives and the score of each enemy. Defining constants can facilitate program maintenance.

Next, let's take a look at the EnemySprite. h header file:

# Include "cocos2d. h" # include "EnemyInfo. h" USING_NS_CC; class EnemySprite: public Node {public: EnemySprite (); // constructor ~ EnemySprite (); // destructor virtual bool init (); // initialization function CREATE_FUNC (EnemySprite); // create FUNCTION macro definition, the system has implemented public: void setEnemyByType (EnemyType enType); // set the enemy's type Sprite * getSprite (); // obtain the created enemy int getLife (); // obtain the void loseLife (); // reduce the Rect getBoundingBox () after being hit by a bullet; // obtain the enemy's outer packing rectangle Point getcurPoint (); // obtain the enemy's position private: Sprite * pEnemySprite; int nLife ;};

The functions mentioned above are described in detail. Next, let's look at the various implementations in the EnemySprite. cpp file:

# Include "EnemySprite. h" EnemySprite: EnemySprite () {} EnemySprite ::~ EnemySprite () {}/*** create different enemy hosts Based on the enemy type */void EnemySprite: setEnemyByType (EnemyType enType) {switch (enType) {case Enemy1: pEnemySprite = Sprite: createWithSpriteFrame (metadata: getInstance ()-> example ("n1.png"); nLife = ENEMY1_MAXLIFE; break; case Enemy2: pEnemySprite = Sprite :: createWithSpriteFrame (SpriteFrameCache: getInstance ()-> getSpriteFrameByName ("n2.png"); nLife = ENEMY2_MAXLI FE; break; case Enemy3: pEnemySprite = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance ()-> Accept ("n3.png"); nLife = break; case Enemy4: pEnemySprite = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance ()-> aggregate ("n_boss.png"); nLife = ENEMY3_MAXLIFE; break; default: return; break ;} this-> addChild (pEnemySprite); pEnemySprite-> setScale (0.6f); Size WinSize = Director: getInstance ()-> getWinSize (); Size enemySize = pEnemySprite-> getContentSize (); // set the random occurrence position of the enemy machine int minX = enemySize. width/2; int maxX = winSize. width-enemySize. width/2; int rangeX = maxX-minX; int actualX = (rand () % rangeX) + minX; this-> setPosition (Point (actualX, winSize. height + enemySize. height/2);} bool EnemySprite: init () {bool pRet = true; if (! Node: init () {pRet = false;} return pRet;}/*** get the created enemy plane genie */Sprite * EnemySprite: getSprite () {return pEnemySprite;}/*** get the life value */int EnemySprite: getLife () {return nLife;}/*** reduce the life value, 0.5 */void EnemySprite: loseLife () {nLife = nLife-0.5;}/*** get current position */Point EnemySprite: getcurPoint () {return this-> getPosition ();}/*** obtain the outer packing rectangle */Rect EnemySprite: getBoundingBox () {Rect rect = pEnemySprite-> boundingBox (); point pos = this-> convertToWorldSpace (rect. origin); Rect enemyRect (pos. x, pos. y, rect. size. width, rect. size. height); return enemyRect ;}

The implementation of the above functions is relatively simple, and the key points are described. the retrieval of Sprite images is obtained using the cache Singleton class, caching images to the game after the first loading can reduce the performance loss caused by image loading each time. The code for using the cache class to create the genie is as follows:

Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("n1.png"));


Well, this article mainly introduces the implementation of the enemy class. After creating the enemy class, you can directly call the implemented functions in this file in GameLayer to create the genie, the functions of each file are independent and the modules are clearly defined to better maintain the project.

Next, we will introduce the main character type Plane.





Aircraft vs. luxury edition

You can use titanium backup to back up and unmount it. You can recover it the next time you install it. In theory, any software supports titanium backup. Trial is recommended. Not just games!

What about the aircraft vs. luxury edition?

A good game. I just played it several times. Easy to use. However, it is challenging to break the limits. Nice.


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