炮塔哲学:敌人,攻击波和路径点
在创建敌人之前,让我们先为它们”铺路”.敌人将沿着一系列的路径点前进,这些路径点互相连接,它们被定义为敌人在你创建的世界中移动的路径.
敌人将在第一个路径点上出现,搜索列表中下一个路径点,然后这样重复下去,直到它们到达最后一个路径点-你的基地!如果这些家伙抵达你的基地,你将收到损伤.
我们使用类模板创建路径点的列表,名字为Waypoint,继承于CCNode.
将Waypoint.h替换为如下内容:
#import "cocos2d.h" #import "HelloWorldLayer.h" @interface Waypoint: CCNode { HelloWorldLayer *theGame; } @property (nonatomic,readwrite) CGPoint myPosition; @property (nonatomic,assign) Waypoint *nextWaypoint; +(id)nodeWithTheGame:(HelloWorldLayer*)_game location:(CGPoint)location; -(id)initWithTheGame:(HelloWorldLayer *)_game location:(CGPoint)location; @end
然后在替换Waypoint.m为以下内容:
#import "Waypoint.h" @implementation Waypoint @synthesize myPosition, nextWaypoint; +(id)nodeWithTheGame:(HelloWorldLayer*)_game location:(CGPoint)location { return [[self alloc] initWithTheGame:_game location:location]; } -(id)initWithTheGame:(HelloWorldLayer *)_game location:(CGPoint)location { if( (self=[super init])) { theGame = _game; [self setPosition:CGPointZero]; myPosition = location; [theGame addChild:self]; } return self; } -(void)draw { ccDrawColor4B(0, 255, 2, 255); ccDrawCircle(myPosition, 6, 360, 30, false); ccDrawCircle(myPosition, 2, 360, 30, false); if(nextWaypoint) ccDrawLine(myPosition, nextWaypoint.myPosition); [super draw]; } @end
首先,代码通过传递HelloWorldLayer对象的引用和一个CGPoint来初始化一个路径点,CGPoint是该路径点的位置.
每一个路径点包括了下一个路径点的引用;它创建了一系列路径点的链接(你以前有认真留意过数据结构课程吗?).每一个路径点”知道”列表中的下一个路径点.通过跟随这些路径点链接,你可以引导敌人到它们最终的目的地.敌人从不会从地图上撤退,它们有点像神风特工队的队员一样(kamikaze warriors).