Search code examples
ioscocos2d-x

pause a specific action on sprite


I've a player of Sprite * type in cocos2dx V3, I want it to run different animation on different time interval, I could not find method to pause and then resume a specific animation(Action). Although I can Pause and Resume all actions of Sprite Simultaneously using _player->pauseSchedulerAndActions().I'm using "CCRepeatForever" actions on sprite, so, I must have to pause one to resume other.Please help to Pause an action by tag or by any other method. Thanks In Advance.


Solution

  • Oops

    I made the assumption that this was Objective-C but @Droppy has informed me that it is not.

    I didn't realise cocos2d-x was different. However, because this is a fairly high level framework the concept behind what I've done in the answer will still work. I'll keep the answer here for now.

    The answer

    It's been a while since I've done any Cocos2D stuff but I can give you the idea.

    Instead of creating an action and repeating it forever you should have a method something like this...

    - (void)twirlAround
    {
        // only create and perform the actions if variable set to YES
        if (self.twirling) {
            // this will do your action once.
            CCAction *twirlAction = // create your twirl action (or whatever it is)
    
            // this will run this function again
            CCAction *repeatAction = [CCActionCallBlock actionWithBlock:^{
                [self twirlAround];
            }];
    
            // put the action and method call in sequence.
            CCActionSequence *sequence = [CCActionSequence actions:@[twirlAction, repeatAction]];
    
            [self runAction:sequence];
        }
    }
    

    This will run repeatedly as long as the twirling property is set to YES.

    So, somewhere else in your code (probably where you are currently adding your repeating action) you can do this...

    self.twirling = YES;
    [self twirlAround];
    

    This will start the repeated twirling.

    To stop it you can then do...

    self.twirling = NO;
    

    This will stop the twirling.

    Alternative method

    - (void)twirlAround
    {
        // this will do your action once.
        CCAction *twirlAction = // create your twirl action (or whatever it is)
    
        // this will run this function again
        CCAction *repeatAction = [CCActionCallBlock actionWithBlock:^{
            if (self.twirling) {
                [self twirlAround];
            }
        }];
    
        // put the action and method call in sequence.
        CCActionSequence *sequence = [CCActionSequence actions:@[twirlAction, repeatAction]];
    
        [self runAction:sequence];
    }