现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为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); ccDrawCircle(mySprite.position,attackRange,360,30,false); [super draw];}@end
该炮塔类包含了一些属性:一个精灵,用来可视化表示一个炮塔;一个便于访问父节点的引用;以及3个变量:
攻击范围:确定炮塔可以攻击多远 伤害:确定炮塔可以造成敌人多少损伤 射击速率:确定炮塔重新装d射击需要间隔多长时间 总结以上是内存溢出为你收集整理的Cocos2D:塔防游戏制作之旅(六)全部内容,希望文章能够帮你解决Cocos2D:塔防游戏制作之旅(六)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)