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

前端之家收集整理的这篇文章主要介绍了Cocos2D:塔防游戏制作之旅(六)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为Tower,继承于CCNode.

替换Tower.h文件为如下内容:

  1. #import "cocos2d.h"
  2. #import "HelloWorldLayer.h"
  3.  
  4. #define kTOWER_COST 300
  5.  
  6. @class HelloWorldLayer,Enemy;
  7.  
  8. @interface Tower: CCNode {
  9. int attackRange;
  10. int damage;
  11. float fireRate;
  12. }
  13.  
  14. @property (nonatomic,weak) HelloWorldLayer *theGame;
  15. @property (nonatomic,strong) CCSprite *mySprite;
  16.  
  17. +(id)nodeWithTheGame:(HelloWorldLayer*)_game location:(CGPoint)location;
  18. -(id)initWithTheGame:(HelloWorldLayer *)_game location:(CGPoint)location;
  19.  
  20. @end

现在将Tower.m替换为如下内容:

  1. #import "Tower.h"
  2.  
  3. @implementation Tower
  4.  
  5. @synthesize mySprite,theGame;
  6.  
  7. +(id) nodeWithTheGame:(HelloWorldLayer*)_game location:(CGPoint)location
  8. {
  9. return [[self alloc] initWithTheGame:_game location:location];
  10. }
  11.  
  12. -(id) initWithTheGame:(HelloWorldLayer *)_game location:(CGPoint)location
  13. {
  14. if( (self=[super init])) {
  15.  
  16. theGame = _game;
  17. attackRange = 70;
  18. damage = 10;
  19. fireRate = 1;
  20.  
  21. mySprite = [CCSprite spriteWithFile:@"tower.png"];
  22. [self addChild:mySprite];
  23.  
  24. [mySprite setPosition:location];
  25.  
  26. [theGame addChild:self];
  27.  
  28. [self scheduleUpdate];
  29.  
  30. }
  31.  
  32. return self;
  33. }
  34.  
  35. -(void)update:(ccTime)dt
  36. {
  37.  
  38. }
  39.  
  40. -(void)draw
  41. {
  42. ccDrawColor4B(255,255,255);
  43. ccDrawCircle(mySprite.position,attackRange,360,30,false);
  44. [super draw];
  45. }
  46.  
  47. @end

该炮塔类包含了一些属性:一个精灵,用来可视化表示一个炮塔;一个便于访问父节点的引用;以及3个变量:

  • 攻击范围:确定炮塔可以攻击多远
  • 伤害:确定炮塔可以造成敌人多少损伤
  • 射击速率:确定炮塔重新装弹射击需要间隔多长时间

猜你在找的Cocos2d-x相关文章