iphoneioscocos2d-iphonegame-ai

Sprite jumping around when i use resumeSchedulerAndActions


Ok I am attempting some AI stuff here and I have been following some Ray Wenderlich tutorials. I have some strange behavior going on. Maybe I am just doing this all wrong... but here you go. When a sprite is within 75 pixels of the target it switches to the Defending AIState and i call pauseSchedulerAndActions and set it to a predetermined safe spot via getDefensePosition method. What I am trying to do is after 2 seconds resume the actions so the sprite will move around again. so I call resumeSchedulerAndActions. Now this just goes through the getDefenseMethod and it moves te sprite between these three places but this is the strange behavior i have two slog calls one before getDefenseMethod and one after the sprite is jumping around from the center of the screen then back to the new 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}

dont understand why it is doing that. Why does it not just start from the position it was in?

- (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");
    }


}

Solution

  • FWIW, I suspect your player object has some CCMove type of actions (which you are pausing). Even though you change the position while paused, when the action resumes, the action sets the position to its current state (startPosition, endPosition, duration, time elapsed since start), which may be quite different from the position you set during the pause.

    not certain of your object model/class structure, but something like this:

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

    this way, you recreate a moveto action that will be executed from spawnPoint, and your player.position is not in contention with a running action. Written from memory, you mileage may vary :)