好的,我在这里尝试一些人工智能的东西,我一直在关注一些 Ray Wenderlich 教程。我有一些奇怪的行为。也许我只是做错了……但是给你。当一个精灵在目标的 75 像素内时,它会切换到防御AIState
,我通过方法调用pauseSchedulerAndActions
并将其设置为预定的安全点getDefensePosition
。我想做的是在 2 秒后恢复动作,这样精灵就会再次移动。所以我打电话resumeSchedulerAndActions
。现在这只是通过getDefenseMethod
,它在这三个地方之间移动精灵,但这是奇怪的行为,我有两个 slog 调用,一个之前getDefenseMethod
和一个之后,精灵从屏幕中心跳来跳去,然后回到新的 spawnPoint:
2013-03-04 20:08:14.897 10-8[2629:c07] before: {217.533, 177.32}
2013-03-04 20:08:14.898 10-8[2629:c07] spawnPoint 1
2013-03-04 20:08:14.899 10-8[2629:c07] after: {100, 100}
不明白为什么要这样做。为什么它不只是从它所在的位置开始?
- (void)execute:(GangMembers *)player {
// Check if should change state
NSArray * enemies = [player.layer enemiesOutsideRange:75 ofPlayer:player];
if (enemies.count > 0) {
NSLog(@"outside range 75");
[player changeState:[[Attacking alloc] init]];
return;
}
[player.layer setPlayer:player attacking:NO];
// Make build decision
[player.layer unschedule:@selector(shoot:)];
[player pauseSchedulerAndActions];
NSLog(@"before: %@", NSStringFromCGPoint(player.position));
[self getDefensePosition];
player.position = spawnPoint;
NSLog(@"after: %@", NSStringFromCGPoint(player.position));
[player performSelector:@selector(resumeSchedulerAndActions) withObject:player afterDelay:2];
}
- (void)getDefensePosition {
// CGSize winSize = [CCDirector sharedDirector].winSize;
int spawnChoice = arc4random() % 3;
spawnPoint = ccp(100, 100);
if(spawnChoice == 0){
spawnPoint = ccp(100, 100);
NSLog(@"spawnPoint 1");
}
else if(spawnChoice == 1){
spawnPoint = ccp(100, 200);
NSLog(@"spawnPoint 2");
}
else {
spawnPoint = ccp(100, 300);
NSLog(@"spawnPoint 3");
}
}