cocos2d-iphoneccaction

Cocos2d - Actions and Animations are not paused


When I do this:

[gameLayer pauseSchedulerAndActions];

Most of the game pauses, but the sprites that are undergoing this action do not pause spinning:

[sprite runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:5.0 angle: 360]]];

Also, those sprites that are running CCAnimations do not stop animating:

CCAnimation *theAnim = [CCAnimation animationWithSpriteFrames:theFrames delay:0.1];
CCSprite *theOverlay = [CCSprite spriteWithSpriteFrameName:@"whatever.png"];                
self.theAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:theAnim]];

How can I get these to pause when the game is paused? I would expect “pauseSchedulerAndActions” to pause actions, but that doesn’t seem to be the case.


Solution

  • pauseSchedulerAndActions is not recursive, so it will only affect actions and schedules on the node you are pausing, not it's children.

    if your scene/layer is shallow, you could probably just get away with looping through the layer's children and calling (pause/resume)SchedulerAndActions on each, otherwise, if you have a deeper graph, you'll probably want to call it recursively. I wrote up a small test and this worked for me:

    -(void) pauseSchedulerAndActions: (BOOL) pause forNodeTree:(id)parentNode shouldIncludeParentNode:(BOOL)includeParent
    {
        if(includeParent)
        {
            (pause) ? [parentNode pauseSchedulerAndActions] : [parentNode resumeSchedulerAndActions];
        }
    
        for( CCNode *cnode in [parentNode children] )
        {
            (pause) ? [cnode pauseSchedulerAndActions] : [cnode resumeSchedulerAndActions];
    
            if(cnode.children.count > 0)
            {
                [self pauseSchedulerAndActions:pause forNodeTree:cnode shouldIncludeParentNode:NO]; // on recurse, don't process parent again
            }
        }
    }
    

    so in your case you could try calling this method and passing in your gameLayer