当前位置: 动力学知识库 > 问答 > 编程问答 >

ccaction - 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.

网友答案:

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

网友答案:

Try [[CCDirector sharedDirector] pause]; It should pause all animations and movements.

分享给朋友:
您可能感兴趣的文章:
随机阅读: