Cocos2D:塔防游戏制作之旅(六)
发布时间:2020-12-14 16:43:47 所属栏目:百科 来源:网络整理
导读:现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为Tower,继承于CCNode. 替换Tower.h文件为如下内容: #import "cocos2d.h" #import "HelloWorldLayer.h" #define kTOWER_COST 300 @class HelloWorldLayer , Enemy ; @interface Tower : CCNode { int at
现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为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个变量:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |