Cocos2D:塔防游戏制作之旅(十四)

前端之家收集整理的这篇文章主要介绍了Cocos2D:塔防游戏制作之旅(十四)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

塔之战:炮塔的攻击

炮塔就位了?检查.敌人前进中?再次检查 - 它们看起来就是如此!看起来到了击溃这些家伙的时候了!这里我们将智能置入炮塔的代码中去.

每一个炮塔检查是否有敌人在其攻击范围.(炮塔一次只能攻击一个敌人.猫猪注)如果有,炮塔将开始向敌人开火直到两件事之一发生:敌人移出攻击范围或者敌人被摧毁.炮塔接着开始寻找其他欠扁的家伙 :]

将它们放到一起,新建新炮塔!你已经有一个防御基础了!

因为敌人和炮塔类相互依赖彼此,你不得不首先更新它们类的头文件,去避免你在修改实现代码时Xcode发生显示错误.

首先,打开Tower.h文件,然后完成以下修改:

  1. // Add some instance variables
  2. BOOL attacking;
  3. Enemy *chosenEnemy;
  4.  
  5. // Add method definition
  6. -(void)targetKilled;

打开Enemy.h文件修改如下:

  1. // Add instance variable
  2. NSMutableArray *attackedBy;
  3.  
  4. // Add method definitions
  5. -(void)getAttacked:(Tower *)attacker;
  6. -(void)gotLostSight:(Tower *)attacker;
  7. -(void)getDamaged:(int)damage;

下一步,回到Tower.m中做如下修改:

  1. // Import Enemy header at the top of the file:
  2. #import "Enemy.h"
  3.  
  4. // Add the following methods
  5. -(void)attackEnemy
  6. {
  7. [self schedule:@selector(shootWeapon) interval:fireRate];
  8. }
  9.  
  10. -(void)chosenEnemyForAttack:(Enemy *)enemy
  11. {
  12. chosenEnemy = nil;
  13. chosenEnemy = enemy;
  14. [self attackEnemy];
  15. [enemy getAttacked:self];
  16. }
  17.  
  18. -(void)shootWeapon
  19. {
  20. CCSprite * bullet = [CCSprite spriteWithFile:@"bullet.png"];
  21. [theGame addChild:bullet];
  22. [bullet setPosition:mySprite.position];
  23. [bullet runAction:[CCSequence actions:
  24. [CCMoveTo actionWithDuration:0.1 position:chosenEnemy.mySprite.position],[CCCallFunc actionWithTarget:self selector:@selector(damageEnemy)],[CCCallFuncN actionWithTarget:self selector:@selector(removeBullet:)],nil]];
  25.  
  26.  
  27. }
  28.  
  29. -(void)removeBullet:(CCSprite *)bullet
  30. {
  31. [bullet.parent removeChild:bullet cleanup:YES];
  32. }
  33.  
  34. -(void)damageEnemy
  35. {
  36. [chosenEnemy getDamaged:damage];
  37. }
  38.  
  39. -(void)targetKilled
  40. {
  41. if(chosenEnemy)
  42. chosenEnemy =nil;
  43.  
  44. [self unschedule:@selector(shootWeapon)];
  45. }
  46.  
  47. -(void)lostSightOfEnemy
  48. {
  49. [chosenEnemy gotLostSight:self];
  50. if(chosenEnemy)
  51. chosenEnemy =nil;
  52.  
  53. [self unschedule:@selector(shootWeapon)];
  54. }

最后,替换之前版本只能怪留下的空白update方法:

  1. -(void)update:(ccTime)dt {
  2. if (chosenEnemy){
  3.  
  4. //We make it turn to target the enemy chosen
  5. CGPoint normalized = ccpNormalize(ccp(chosenEnemy.mySprite.position.x-mySprite.position.x,chosenEnemy.mySprite.position.y-mySprite.position.y));
  6. mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x))+90;
  7.  
  8. if(![theGame circle:mySprite.position withRadius:attackRange
  9. collisionWithCircle:chosenEnemy.mySprite.position collisionCircleRadius:1])
  10. {
  11. [self lostSightOfEnemy];
  12. }
  13. } else {
  14. for(Enemy * enemy in theGame.enemies)
  15. {
  16. if([theGame circle:mySprite.position withRadius:attackRange
  17. collisionWithCircle:enemy.mySprite.position collisionCircleRadius:1])
  18. {
  19. [self chosenEnemyForAttack:enemy];
  20. break;
  21. }
  22. }
  23. }
  24. }

猜你在找的Cocos2d-x相关文章