Cocos2D: Anti-tower game production Tour (10)
In the end, the draw method shows where these path points are placed and draw connections between the path points. They are only used for debugging. A finished game should not draw enemy paths-it is too easy for players! <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/qkhlbGxvV29ybGRMYXllci5osqLH0sztvNPS1M/CyvTQ1Do8L3A + DQo8cHJlIGNsYXNzPQ = "brush: java;"> @property (nonatomic,strong) NSMutableArray *waypoints;
Next, add the following code to the HelloWorldLayer. m file:
//At the top of the file:#import "Waypoint.h"// Add synthesise@synthesize waypoints;//Add this method -(void)addWaypoints{ waypoints = [[NSMutableArray alloc] init]; Waypoint * waypoint1 = [Waypoint nodeWithTheGame:self location:ccp(420,35)]; [waypoints addObject:waypoint1]; Waypoint * waypoint2 = [Waypoint nodeWithTheGame:self location:ccp(35,35)]; [waypoints addObject:waypoint2]; waypoint2.nextWaypoint =waypoint1; Waypoint * waypoint3 = [Waypoint nodeWithTheGame:self location:ccp(35,130)]; [waypoints addObject:waypoint3]; waypoint3.nextWaypoint =waypoint2; Waypoint * waypoint4 = [Waypoint nodeWithTheGame:self location:ccp(445,130)]; [waypoints addObject:waypoint4]; waypoint4.nextWaypoint =waypoint3; Waypoint * waypoint5 = [Waypoint nodeWithTheGame:self location:ccp(445,220)]; [waypoints addObject:waypoint5]; waypoint5.nextWaypoint =waypoint4; Waypoint * waypoint6 = [Waypoint nodeWithTheGame:self location:ccp(-40,220)]; [waypoints addObject:waypoint6]; waypoint6.nextWaypoint =waypoint5;}// At the end of init:// 4 - Add waypoints[self addWaypoints];
Compile and run the game. You will see the following picture:
There are 6 path points on the map; the enemy will follow the paths they constitute. Before you place 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 warning from 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(CGPoint *poli, int points, BOOL 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.y; float distance = sqrt(xdif*xdif+ydif*ydif); if(distance <= radius+radiusTwo) return YES; return NO;}
The collisionWithCircle method will help us determine the collision or intersection between the two circles. It will determine whether the enemy has reached the path point, and also check whether the enemy has entered the attack range of the turret.