您当前的位置: 首页 >  游戏

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

发布时间:2015-11-16 21:27:47 ,浏览量:0

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

  • 攻击范围:确定炮塔可以攻击多远
  • 伤害:确定炮塔可以造成敌人多少损伤
  • 射击速率:确定炮塔重新装弹射击需要间隔多长时间
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    110453博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0638s