最终,draw方法显示这些路径点被放置在哪里,并且绘制出路径点之间的连线,它们仅仅被用作调试.一个成品游戏不应该绘制敌人的路径 - 那对于玩家来说太过容易了!
创建路径点的列表.打开HelloWorldLayer.h并且添加以下属性:
@property (nonatomic,strong) NSMutableArray *waypoints;
下一步,添加以下代码到HelloWorldLayer.m文件中:
//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];
编译运行游戏,你将看到如下画面:
在地图上有6个路径点;敌人将跟随它们组成的路径.在你将坏家伙们放置在你游戏中之前,你需要添加一些帮助方法.
首先,在头文件中添加方法定义,以便其他类可以访问该方法而不招至编译器的警告.
打开HelloWorldLayer.h文件,在@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;
下一步,打开HelloWorldLayer.m文件,同样在@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;
}
该collisionWithCircle方法将帮助我们确定两个圆的碰撞或者说是相交.它将确定敌人是否到达路径点,同样也可以检查敌人是否进入了炮塔的攻击范围.