Finally, the draw method shows where these path points are placed, and plots the lines between the path points, which are only used for debugging purposes. A finished game should not draw the enemy's path-that's too easy for the player!
Creates a list of path points. Open HelloWorldLayer.h and add the following properties:
@property (nonatomic,strongNSMutableArray *waypoints;
Next, add the following code to the HELLOWORLDLAYER.M file:
//at The top of the file:#import "Waypoint.h" //ADD synthesise@synthesizewaypoints;//add This method-(void) addwaypoints{waypoints = [[NsmutablearrayALLOC] init]; Waypoint * Waypoint1 = [Waypoint nodewiththegame: SelfLOCATION:CCP (420, *)]; [Waypoints addobject:waypoint1]; Waypoint * Waypoint2 = [Waypoint nodewiththegame: SelfLOCATION:CCP ( *, *)]; [Waypoints Addobject:waypoint2]; Waypoint2. Nextwaypoint=waypoint1; Waypoint * Waypoint3 = [Waypoint nodewiththegame: SelfLOCATION:CCP ( *, the)]; [Waypoints Addobject:waypoint3]; Waypoint3. Nextwaypoint=waypoint2; Waypoint * Waypoint4 = [Waypoint nodewiththegame: SelfLOCATION:CCP (445, the)]; [Waypoints ADDOBJECT:WAYPOINT4]; Waypoint4. Nextwaypoint=waypoint3; Waypoint * WAYPOINT5 = [Waypoint nodewiththegame: SelfLOCATION:CCP (445, -)]; [Waypoints ADDOBJECT:WAYPOINT5]; Waypoint5. Nextwaypoint=WAYPOINT4; Waypoint * Waypoint6 = [Waypoint nodewiththegame: SelfLOCATION:CCP (- +, -)]; [Waypoints ADDOBJECT:WAYPOINT6]; Waypoint6. Nextwaypoint=WAYPOINT5;}// at the end of the init://4-add waypoints[ SelfAddwaypoints];
To compile and run the game, you will see the following screen:
There are 6 path points on the map; the enemy will follow their path. Before you put the bad guys in your game, you need to add some help methods.
First, add a method definition to the header file so that other classes can access the method without the warning of the compiler.
Open the HelloWorldLayer.h file and add the following code before @end:
-(BOOL)circle:(CGPoint)circlePoint withRadius:(float)radius collisionWithCircle:(CGPoint)circlePointTwo collisionCircleRadius:(float)radiusTwo;void ccFillPoly(CGPointintBOOL closePolygon);-(void) enemyGotKilled;-(void) getHpDamage;
Next, open the Helloworldlayer.m file and add the following code before @end:
-(bool ) Circle: ( Cgpoint ) circlepoint Withradius: (float ) Radius collisionwithcircle: (cgpoint ) circlepointtwo Collisioncircleradius: (float ) radiustwo {float xdif = Circlepoint.x -Circlepointtwo.x ; float ydif = Circlepoint.y -circlepointtwo< Span class= "hljs-variable" >.y ; float distance = sqrt (xdif*xdif+ydif*ydif); if (Distance <= radius+radiustwo) return yes ; return no ;}
The Collisionwithcircle method will help us determine whether two circles collide or intersect. It will determine if the enemy has reached the point of the path, and can also check if the enemy has entered the turret's attack range.
COCOS2D: Tower Defense game Making Tour (10)