0

I would like to stop function like this:

     - (void)update:(NSTimeInterval)currentTime
    {

    if (self.lastUpdateTimeInterval)
    {
       _dt1 = currentTime - _lastUpdateTimeInterval;

    }
    else
    {
      _dt1 = 0;
    }

    CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
   self.lastUpdateTimeInterval = currentTime;

    [self moveObject];
  }

I now timers stop like: [timer1 invalidate], but how to stop function that is running on timer interval?

user3215624
  • 55
  • 11
  • Can you show the function? – Greg Mar 23 '14 at 12:22
  • You can stop timer by calling [timer invalidate]; timer = nil; but it just stop calling the function, if the function has started some action, for example animation or other async job you have to stop it separately. It's difficult to say how to stop it without knowing what do you want to stop. So the question is what do you want to stop? – Greg Mar 23 '14 at 12:34
  • U have right. I want to stop my Sprite Kit action. Action is defined in a function that crates sprite kit node. But this function is called wit this timer. So when i stop it once, it will crete it again. – user3215624 Mar 23 '14 at 12:39

1 Answers1

0

If you want to stop the animation you can call removeAllActions on your sprite object:

[_sprite removeAllActions];

But I think you need to also run the animation just if the sprite is not running it already. To do that you can use:

if (![_sprite hasActions])
{
   [_sprite runAction:action];
}

If you want to stop just one animation (not all) you can use runAction:withKey: method when you run action and you can see if the action is running by calling actionForKey: or you can stop it by calling removeActionForKey.

Greg
  • 25,317
  • 6
  • 53
  • 62