Cocos2D: Anti-tower game production Tour (6)
Now, create a new class to represent the turret. Add a new class file named Tower, inherited from CCNode.
Replace Tower. h with the following content:
#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
Replace Tower. m with the following content:
#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
This turret class contains some attributes: A genie used to visually represent a turret, a reference for easy access to the parent node, and three variables: