COCOS2D: Tower Defense Game Making Tour (16)

Source: Internet
Author: User

Compile and run your app and place some turrets on your map! You will see how the turret immediately begins to attack when the enemy moves, such as the range of attack, and the enemy's blood strips will be reduced as the attacks continue to know that they are being humanely destroyed! The victory is coming!

Oh! Okay, here are just a few details that you can get a full feature of the Tower defense game now! Sound should be a good try. And despite being invincible and extremely wealthy, your base should have the ability to fight continuously-and you need to limit the player's gold supply.

Shining turret: Gotta Polish It all!

Start implementing fate that show the rest of the player-and what happens when the player fails!

Open HelloWorldLayer.h and add the following 3 instance variables:

int*ui_hp_lbl;BOOL gameEnded;

PLAYERHP determines how many lives the player has, and cclabelbmfont tags are used to display their number. Gameended is set at the end of the game! Also add the following method definitions:

-(void)doGameOver;

Now open the HELLOWORLDLAYER.M file and complete the following modifications:

At theEndOf Init, add the following lines of code://7-Player LIVESPLAYERHP =5; ui_hp_lbl = [Cclabelbmfont labelwithstring:[nsstring stringwithformat:@"HP:%d", PLAYERHP] fntfile:@"Font_red_14.fnt"]; [Self ADDCHILD:UI_HP_LBL z:Ten]; [Ui_hp_lbl SETPOSITION:CCP ( *, winsize.height- A)];//Add The following methods-(void) Gethpdamage {PLAYERHP--;[Ui_hp_lbl setstring:[nsstring stringwithformat:@"HP:%d", PLAYERHP]];if(PLAYERHP <=0) {[Self dogameover]; }}-(void) Dogameover {if(!gameended) {gameended = YES;[[Ccdirector shareddirector] Replacescene:[cctransitionrotozoom transitionwithduration:1 Scene:[helloworldlayer scene]]]; }}

This adds a way to reduce the player's life, update the label's display, and check if the player is running out of life. If so, the game is over.

COCOS2D: Tower Defense Game Making Tour (16)

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.