5

In my game, I am using SKAction repeatActionForever: method to periodically call some method with performSelector: . As I seen, there is no way to stop this. I tried to stop via removeActionForKey: or removeAllActions - no results. I don't want to call this action recursively , so I need help.

UPDATE: My SKAction code

levelTimer=[SKAction repeatActionForever:[SKAction sequence:@[[SKAction waitForDuration:30.0],[SKAction performSelector:@selector(moveToNextLevel) onTarget:self]]]];
[self runAction:levelTimer withKey:@"levelTimerAction"];
Igor Prusyazhnyuk
  • 133
  • 2
  • 14
  • 29

1 Answers1

4

The repeatActionForever method should be called on the node which the SKAction is running on, so:

 [sprite removeActionForKey:@"forevercalleraction"]; 

Assume you add the action like this, and the sprite variable is not locally added (declared in .h):

 [sprite runAction:repeatPerformSelectorAction withKey:@"forevercalleraction"];

To sum the above, check these:

  1. the node which runs the action is declared in .h
  2. you use the same key string for adding and removing the action
  3. you try to call the removeActionForKey on the same node which declared in 1.

Hope it helps!

nzs
  • 3,252
  • 17
  • 22
  • See my updated question. I add action not to a sprite. I add this to my SKScene. And I tried to remove it via [self removeActionForKey:@"levelTimerAction"]; – Igor Prusyazhnyuk Jun 23 '14 at 09:49
  • 1
    Is it necessary adding the SKScene, since you can create a simple node and add this action to that invisible node..? – nzs Jun 23 '14 at 09:55
  • Thanks, now it works :) Invisible node that run my actions. Hm, this nonsense makes sense – Igor Prusyazhnyuk Jun 23 '14 at 10:01
  • If you use this approach it will work. I did just this very thing last night. In general it's best to always use a key string so you can reference the action later or find out if it's nil ( not the rest all ). It's a good habit. You can create and extern NSString Constants for your keys, plop them into dictionaries or arrays or sets. Then do a lot easily. – uchuugaka Jun 23 '14 at 10:01