COCOS2D: Tower Defense Game Making Tour (11)

Source: Internet
Author: User
Tags addchild

It's time to put some bad guys in the mix!

Open HelloWorldLayer.h and add the following code:

// Add these instance variablesint wave;CCLabelBMFont *ui_wave_lbl;// Add the following property to the properties section@property (nonatomic,strongNSMutableArray *enemies;

Modify the Helloworldlayer.m file as follows:

// Synthesize enemies@synthesize enemies;

Now it's time to create classes that hold enemy information and manage how they move on the screen. Create a new class with the name enemy and inherit from Ccnode.

Replace the Enemy.h with the following:

#import "Cocos2d.h" #import "HelloWorldLayer.h"  @class helloworldlayer, waypoint, Tower; @interface Enemy: ccnode {    CgpointMyposition;intMAXHP;intCURRENTHP;floatWalkingspeed; Waypoint *destinationwaypoint;BOOLActive;}@property(nonatomic,Assign) Helloworldlayer *thegame;@property(nonatomic,Assign) Ccsprite *mysprite;+ (ID) Nodewiththegame: (helloworldlayer*) _game;-(ID) Initwiththegame: (Helloworldlayer *) _game;-(void) doactivate;-(void) getremoved;@end

Now replace the ENEMY.M file with the following:

#import "Enemy.h" #import "Tower.h" #import "Waypoint.h" #define Health_bar_width#define HEALTH_BAR_ORIGIN-10 @implementation Enemy @synthesizeMySprite, thegame;+ (ID) Nodewiththegame: (helloworldlayer*) _game {return[[ SelfAlloc] initwiththegame:_game];} -(ID) Initwiththegame: (Helloworldlayer *) _game {if(( Self=[SuperInit]) {thegame = _game; MAXHP = +;        CURRENTHP = MAXHP; Active =NO; Walkingspeed =0.5; MySprite = [Ccsprite spritewithfile:@"Enemy.png"]; [ SelfAddchild:mysprite]; Waypoint * Waypoint = (waypoint *) [TheGame. waypointsObjectatindex: ([thegame. waypointscount]-1)]; Destinationwaypoint = Waypoint. Nextwaypoint;Cgpointpos = Waypoint. Myposition;        Myposition = pos;        [MySprite Setposition:pos]; [TheGame AddChild: Self]; [ SelfScheduleupdate]; }return  Self;} -(void) doactivate{active =YES;} -(void) Update: (cctime) dt{if(!active)return;if([TheGame circle:myposition Withradius:1Collisionwithcircle:destinationwaypoint. MypositionCollisioncircleradius:1])    {if(Destinationwaypoint. Nextwaypoint) {Destinationwaypoint = Destinationwaypoint. Nextwaypoint; }Else{//reached the end of the road. Damage the player[TheGame Gethpdamage]; [ SelfGetremoved]; }    }CgpointTargetPoint = Destinationwaypoint. Myposition;floatMovementspeed = Walkingspeed;Cgpointnormalized = Ccpnormalize (CCP (targetpoint. x-myposition. x, TargetPoint. Y-myposition. Y)); MySprite. Rotation= Cc_radians_to_degrees (atan2 (normalized. Y,-normalized. x)); Myposition = CCP (myposition. x+normalized. x* Movementspeed, Myposition. Y+normalized. Y* movementspeed); [MySprite setposition:myposition];} -(void) getremoved{[ Self. ParentRemoveChild: SelfCleanupYES]; [TheGame. EnemiesRemoveobject: Self];//notify The game that we killed a enemy so we can check if we can send another wave[TheGame enemygotkilled];} -(void) draw{Ccdrawsolidrect (CCP (myposition. x+health_bar_origin, Myposition. Y+ -), CCP (myposition. x+health_bar_origin+health_bar_width, Myposition. Y+ -), ccc4f (1.0,0,0,1.0)); Ccdrawsolidrect (CCP (myposition. x+health_bar_origin, Myposition. Y+ -), CCP (myposition. x+health_bar_origin + (float) (CURRENTHP * health_bar_width)/maxhp, myposition. Y+ -), ccc4f (0,1.0,0,1.0));}@end

COCOS2D: Tower Defense Game Making Tour (11)

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.