Cocos2d - Actions and Animations are not paused

大兔子大兔子 提交于 2019-12-11 06:26:18

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/19056101/cocos2d-actions-and-animations-are-not-paused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!