Cocos2d - 动作和动画没有暂停动作、动画、Cocos2d

2023-09-06 14:58:22 作者:渡我不渡他

当我这样做时:

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

此外,那些正在运行 CCAnimations 的精灵不会停止动画:

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

当游戏暂停时,我怎样才能让这些暂停?我希望pauseSchedulerAndActions"能够暂停操作,但似乎并非如此.

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 不是递归的,所以它只会影响你正在暂停的节点上的动作和调度,而不是它的子节点.

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

如果您的场景/层很浅,您可能只需遍历层的子层并在每个层上调用 (pause/resume)SchedulerAndActions,否则,如果您有更深的图,您可能想要调用它递归地.我写了一个小测试,这对我有用:

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
        }
    }
}

所以在您的情况下,您可以尝试调用此方法并传入您的 gameLayer

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