Cocos2D: tower anti-DDoS game production Tour (12)

Source: Internet
Author: User

Cocos2D: tower anti-DDoS game production Tour (12)

The above code block is quite intuitive-but it is a little detailed.

First, the enemy initializes by passing a reference to the HelloWorldLayer object. In the init method, a few important variables are set:

MaxHP: defines how many battles an enemy has (Tough guy, eh ?) WakingSpeed: defines how fast the enemy moves. mySprite: stores the visualized representation of the enemy. destinationWaypoint: stores references of the next path.

The update method is the place where the magic happens is actually a witness. It is called every frame and first checks whether it has reached the destination path, the collisionWithCircle method you wrote earlier is used. if it reaches, it will move toward the next path point-unless the enemy reaches the destination or is destroyed by the player.

Then it moves the Genie along the straight line to the target path point and advances at its own speed. It uses the following algorithm to achieve this purpose:

Calculate a vector point from the current position to the target position, and set its length to 1 for processing (normalized variable ). multiply the normalized vector by the moving speed to get the moving distance in one frame. accumulate to the current location and get the new location.

In the end, the draw method implements a blood strip on top of the Genie. It then draws a red background and overwrites a green rectangle representing the current enemy's HP.

Now that the Enemy class is complete, you can display them on the screen!

Switch to the HelloWorldLayer. m file and complete the following modifications:

//At the top of the file:#import "Enemy.h"//Add the following methods:-(BOOL)loadWave {    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Waves" ofType:@"plist"];    NSArray * waveData = [NSArray arrayWithContentsOfFile:plistPath];    if(wave >= [waveData count])    {        return NO;    }    NSArray * currentWaveData =[NSArray arrayWithArray:[waveData objectAtIndex:wave]];    for(NSDictionary * enemyData in currentWaveData)    {        Enemy * enemy = [Enemy nodeWithTheGame:self];        [enemies addObject:enemy];        [enemy schedule:@selector(doActivate)                interval:[[enemyData objectForKey:@"spawnTime"]floatValue]];    }    wave++;    [ui_wave_lbl setString:[NSString stringWithFormat:@"WAVE: %d",wave]];    return YES;}-(void)enemyGotKilled {    if ([enemies count]<=0) //If there are no more enemies.    {        if(![self loadWave])        {            NSLog(@"You win!");            [[CCDirector sharedDirector] replaceScene:[CCTransitionSplitCols                                                       transitionWithDuration:1                                                       scene:[HelloWorldLayer scene]]];        }    }}// Add the following to the end of the init method:// 5 - Add enemiesenemies = [[NSMutableArray alloc] init];[self loadWave];// 6 - Create wave labelui_wave_lbl = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"WAVE: %d",wave]                                             fntFile:@"font_red_14.fnt"];[self addChild:ui_wave_lbl z:10];[ui_wave_lbl setPosition:ccp(400,winSize.height-12)];[ui_wave_lbl setAnchorPoint:ccp(0,0.5)];

All these code changes need some explanation. The most important part is the loadWave method, which reads data from the Waves. plist file.

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.