2014-04-14 7 views
0

두 개의 적을 공격 모드로 설정하길 원하지만 추가 된 마지막 적만 공격 모드로 설정됩니다.My Sprite AI가 올바르게 작동하지 않습니다. 왜?

이 문제가 발생합니까? 모든 팁이나 제안을 부탁드립니다. 더 많은 코드가 필요하면 알려주십시오.

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

답변

0

루프를 viewDidLoad에서 확인하십시오. 먼저 iVar를 루프 변수로 사용합니다. 아마도 당신이 원하는 것이 아닙니다. 둘째, 반복 할 때마다 self.enemy을 할당하지만 루프가 완료된 후에 enemyAttack을 호출합니다.

또한 enemyAttack은 매개 변수를 사용하지 않으므로 내부 상태를 사용합니다. 루프가 모든 객체에 대해 반복 한 후에 호출되므로 self.enemy은 컬렉션의 마지막 객체가됩니다 (컬렉션에 항목이있는 경우).

따라서 마지막 항목이 적으로 활성화 된 것을 볼 수 있습니다.

0

[self enemyAttack]; 호출을 for 루프에 넣으려고 했습니까?