0

我希望将我的两个敌人设置为攻击模式,但是因为它只有最后一个添加的敌人被设置为攻击模式。

有没有办法解决?任何提示或建议表示赞赏。如果您需要更多代码,请告诉我。

  -(void)ViewDidLoad {
  for (_enemyPoint in [self.enemyGroup objects]) {
  self.enemy = [[CCSprite alloc] initWithFile:@"Icon.png"];
  self.enemy.scale = 32.0f/57.0f;
  self.enemy.position = CGPointMake([_enemyPoint[@"x"] integerValue],    [_enemyPoint[@"y"] integerValue]);
  [self addChild:self.enemy];
    }
  self.pathfinder = [HUMAStarPathfinder pathfinderWithTileMapSize:self.tileMap.mapSize
                                                         tileSize:self.tileMap.tileSize
                                                           delegate:self];
    [self enemyAttack];

                    }


  - (void)enemyAttack{

self.epath = [self.pathfinder findPathFromStart:self.enemy.position
                                          toTarget:self.player.position];
self.eactions = [NSMutableArray array];


for (_epointValueInPath in self.epath) {
   self.epoint = _epointValueInPath.CGPointValue;

   self.emoveTo = [CCMoveTo actionWithDuration:1.0f position:self.epoint];
    [self.eactions addObject:self.emoveTo];

}

self.esequence = [CCSequence actionWithArray:self.eactions];
[self.enemy runAction:self.esequence];
  }
4

2 回答 2

0

看看你的循环viewDidLoad。首先,您使用 iVar 作为循环变量。可能不是你想要的。其次,您self.enemy在每次迭代中分配,但enemyAttack在循环完成后调用。

此外,enemyAttack不带任何参数,因此它使用内部状态。因为它是在循环遍历所有对象之后调用的,所以它self.enemy总是集合中的最后一个对象(如果集合中有任何对象)。

因此,您只看到最后一个项目被激活为敌人也就不足为奇了。

于 2014-04-14T01:53:41.877 回答
0

您是否尝试过将 [self enemyAttack];调用放入for循环中?

于 2014-04-14T01:53:46.750 回答