Search code examples
iossprite-kitskaction

Stop repeatActionForever in Sprite Kit


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

Solution

  • 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!