现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为Tower,继承于CCNode.
替换Tower.h文件为如下内容:
#import "cocos2d.h"
#import "HelloWorldLayer.h"
#define kTOWER_COST 300
@class HelloWorldLayer, Enemy;
@interface Tower: CCNode {
int attackRange;
int damage;
float fireRate;
}
@property (nonatomic,weak) HelloWorldLayer *theGame;
@property (nonatomic,strong) CCSprite *mySprite;
+(id)nodeWithTheGame:(HelloWorldLayer*)_game location:(CGPoint)location;
-(id)initWithTheGame:(HelloWorldLayer *)_game location:(CGPoint)location;
@end
现在将Tower.m替换为如下内容:
#import "Tower.h"
@implementation Tower
@synthesize mySprite,theGame;
+(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;
attackRange = 70;
damage = 10;
fireRate = 1;
mySprite = [CCSprite spriteWithFile:@"tower.png"];
[self addChild:mySprite];
[mySprite setPosition:location];
[theGame addChild:self];
[self scheduleUpdate];
}
return self;
}
-(void)update:(ccTime)dt
{
}
-(void)draw
{
ccDrawColor4B(255, 255, 255, 255);
ccDrawCircle(mySprite.position, attackRange, 360, 30, false);
[super draw];
}
@end
该炮塔类包含了一些属性:一个精灵,用来可视化表示一个炮塔;一个便于访问父节点的引用;以及3个变量:
- 攻击范围:确定炮塔可以攻击多远
- 伤害:确定炮塔可以造成敌人多少损伤
- 射击速率:确定炮塔重新装弹射击需要间隔多长时间