Cocos2D: Anti-tower game production Tour (16th)

Source: Internet
Author: User

Cocos2D: Anti-tower game production Tour (16th)

Compile and run your app and place some turret on your map! You will see how the turret starts an attack immediately when the enemy moves, such as the attack range, and the enemy's blood strip will know that they are destroyed by humanity as the attack decreases! Victory is coming soon!

Oh! Okay, there are only a few details that have not yet been implemented, so you can get a complete anti-tower game! Sound effects should be a good attempt, and even though they are invincible and extremely wealthy, your base should be able to continuously resist attacks-and you need to limit the supply of gold coins to players.

Shining turret: Gotta Polish It All!

Start to display the remaining player's life-and what happens when the player fails!

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

int playerHp;CCLabelBMFont *ui_hp_lbl;BOOL gameEnded;

PlayerHP determines the number of players' lives. The CCLabelBMFont tag is used to display the number of players. gameEnded is set at the end of the game! Add the following method definition:

-(void)doGameOver;

Open the HelloWorldLayer. m file and complete the following changes:

// At the end of 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:10];[ui_hp_lbl setPosition:ccp(35,winSize.height-12)];// 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]]];    }}

Here we have added a method to reduce the player's life, update the tag display, and check whether the player has consumed all his life. If yes, the game ends.

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.