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

Yes,貌似添加了好多的代码啊 ;],在你添加更多代码时,你可能注意到一些Xcode中的一些警告.首先你先忽略这些警告,我们先添加少量最终缺失的部分,然后再来解释上面代码做了什么!

在Enemy.m中修改如下:

// Add the following at the beginning of initWithTheGame: (within the "if" condition)
attackedBy = [[NSMutableArray alloc] initWithCapacity:5];

// replace the contents of getRemoved method with the following:
-(void)getRemoved
{
    for(Tower * attacker in attackedBy)
    {
        [attacker targetKilled];
    }

    [self.parent removeChild:self cleanup:YES];
    [theGame.enemies removeObject:self];

    //Notify the game that we killed an enemy so we can check if we can send another wave
    [theGame enemyGotKilled];
}

// Add the following methods
-(void)getAttacked:(Tower *)attacker
{
    [attackedBy addObject:attacker];
}

-(void)gotLostSight:(Tower *)attacker
{
    [attackedBy removeObject:attacker];
}

-(void)getDamaged:(int)damage
{
    currentHp -=damage;
    if(currentHp <=0)
    {
        [self getRemoved];
    }
}

代码中最重要的部分是Tower中的update方法.炮塔将定时检查是否敌人进入了攻击范围,如果是,我们的炮塔将旋转然后开始向敌人开火.

一旦一个敌人被标记为被攻击,一个方法将被调度执行用之前初始的攻击频率去发射子弹.轮流的,每个敌人保持攻击自身炮塔的一个列表,所以当该敌人被摧毁时那些炮塔可以得到信号去停止攻击.

大佬总结

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

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

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