您的位置:首页 > 移动开发 > Cocos引擎

Cocos2D:塔防游戏制作之旅(六)

2015-11-16 21:27 288 查看
现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为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个变量:

攻击范围:确定炮塔可以攻击多远

伤害:确定炮塔可以造成敌人多少损伤

射击速率:确定炮塔重新装弹射击需要间隔多长时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: