0

I have this method for my AI to use to fire upon the player. When the AI bot is killed it calls [self removeAllActions] on itself. Also the reference to the action (self.shootAction) is set to nil. However, the projectiles still fire once the bot is dead and waiting to respawn. My guess is that this method gets called just before the bot is killed. At that time the bot is invisible and inactive. Is there any way to stop these actions or runBlocks from executing? I tried using a weak reference to self, but that did nothing.

- (void)shootToward:(CGPoint)position;
{
    CGPoint enemyDirection;
    enemyDirection = ccpNormalize(ccpSub(position, self.position));

    SKAction *wait = [SKAction waitForDuration:0.5f];
    SKAction *idle = [SKAction runBlock:^{
        [self idle];
    }];
    SKAction *walkTowardsEnemy = [SKAction runBlock:^{
        [self walkWithDirection:enemyDirection];
    }];
    SKAction *fireAtEnemy = [SKAction runBlock:^{
        [self.delegate fire:self.position
                inDirection:enemyDirection owner:self projNum:0];
    }];
    SKAction *doneShooting = [SKAction runBlock:^{
        [self doneShooting];
    }];

    [self idle];

    self.shootAction = [SKAction sequence:@[walkTowardsEnemy, wait, idle, wait, fireAtEnemy, wait, doneShooting, idle]];

    [self runAction:self.shootAction withKey:@"shootAction"];
    self.shooting = YES;
}
Robert Wasmann
  • 897
  • 7
  • 17
  • I even replaced the blocks with "encodable lightweight objects" as per http://stackoverflow.com/questions/35249269/what-are-good-ways-to-work-around-the-encoding-limitation-of-skaction-code-block yet the problem still persists. – Robert Wasmann Mar 16 '16 at 22:35
  • Solved: Problem turned out to be caused by an NSTimer that I had failed to invalidate when the bot died. It was then resetting a boolean allowing the bot to shoot. – Robert Wasmann Mar 17 '16 at 01:09
  • 1
    shouldnt use NSTimer at all in spritekit – hamobi Mar 17 '16 at 07:03
  • It would have done the same either way. I have some things that need to fire regardless of in game time passage. – Robert Wasmann Mar 18 '16 at 17:52

0 Answers0