当我使用resumeSchedulerAndActions时,精灵跳了起来

时间:2013-03-05 04:26:46

标签: iphone ios cocos2d-iphone game-ai

好的我正在尝试一些人工智能的东西,我一直在关注一些Ray Wenderlich教程。我有一些奇怪的行为。也许我只是做错了......但是你走了。当精灵在目标的75个像素范围内时,它会切换到防御AIState,我会调用pauseSchedulerAndActions并通过getDefensePosition方法将其设置为预定的安全点。我想要做的是在2秒后恢复动作,以便精灵再次移动。所以我打电话给resumeSchedulerAndActions。现在这只是通过getDefenseMethod并且它在这三个地方之间移动te精灵但是这是奇怪的行为我在getDefenseMethod之前有两个slog调用,并且在精灵从中心跳转之后有一个调用然后屏幕返回新的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");
    }


}

1 个答案:

答案 0 :(得分:2)

FWIW,我怀疑你的玩家对象有一些CCMove类型的动作(你正在暂停)。即使您在暂停时更改位置,当操作恢复时,操作会将位置设置为当前状态(startPosition,endPosition,持续时间,自开始以来经过的时间),这可能与您在暂停期间设置的位置完全不同。

不确定你的对象模型/类结构,但是这样的东西:

    [player stopAllActions];
    player.position = spawnPoint;
    [player runAction: [CCSequence actions:
        [CCDelayTime actionWithDuration:2.0],
        [CCMoveTo actionWithDuration:arc4random()%5+1 position: randomPoint], 
         [CCCallBlock actionWithBlock:^{ [self performSelector:@selector(moveRandom:) withObject:s afterDelay:0.5]; }], 
        nil]
    ];
通过这种方式,您可以重新创建将从spawnPoint执行的moveto操作,并且您的player.position不会与正在运行的操作争用。从记忆中写下,你的里程可能会有所不同:)