Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2D:塔防游戏制作之旅(六)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

大佬总结

以上是大佬教程为你收集整理的Cocos2D:塔防游戏制作之旅(六)全部内容,希望文章能够帮你解决Cocos2D:塔防游戏制作之旅(六)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。