当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - My Sprite AI is working incorrectly. Why?

问题描述:

I want my two enemies to be set on attack mode, however as it stands only the last enemy added is being set on attack mode.

Is there any way around this? Any tips or suggestions is appreciated. If you need more code please let me know.

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

}

网友答案:

Look at your loop in viewDidLoad. First, you use an iVar as the loop variable. Probably not what you want. Second, you assign self.enemy in each iteration, but you call enemyAttack after the loop has completed.

Further, enemyAttack does not take any parameters, so it uses internal state. Since it is called after the loop has iterated over all objects, self.enemy will always be the last object in the collection (if there is anything in the collection).

Thus, it is not surprising that you only see the last item being activated as an enemy.

网友答案:

Have you tried to put the [self enemyAttack]; invocation inside the for loop?

分享给朋友:
您可能感兴趣的文章:
随机阅读: