【问题标题】:My Sprite AI is working incorrectly. Why?我的 Sprite AI 工作不正常。为什么?
【发布时间】:2014-05-27 20:25:25
【问题描述】:

我希望我的两个敌人设置为攻击模式,但目前只有最后一个添加的敌人设置为攻击模式。

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

  -(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];
  }

【问题讨论】:

    标签: ios objective-c cocos2d-iphone nsdictionary tmx


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        相关资源
        最近更新 更多