How to wait until current action finish

Hello,

I have a class that inherits from Sprite, this class has a function that is running everytime to simulate automovement. This is more or less the class I have right now:


myClass::myClass()
{
    initWithSpriteFrameName("ball_01.png");
    this->makeRandomMovement();
}

void myClass::moveLeft(){
    // ... ... ... prepare  animations

    auto cb = CallFunc::create( [this]() {
        this->makeRandomMovement();
    });
    auto seq = Sequence::create(other_animations, cb, nullptr);
    this->runAction(seq);
}

void myClass::jumpTwice(){
    auto jump = JumpBy::create(1.0, Vec2(0, 0), 40, 1);    
    auto cb = CallFunc::create( [this]() {
        this->makeRandomMovement();
    });    
    auto seq = Sequence::create(jump, cb, nullptr);    
    this->runAction(seq); 
}

void myClass::makeRandomMovement()
{
    int random_int = rand() % 7;
    switch ( random_int ) {
        case 0:
            moveLeft();
            break;
        default:
            jumpTwice();            
            break;
    }
}
void myClass::jumpToPosition(cocos2d::Vec2 position){
        this->unscheduleAllCallbacks();
        // I tried this but it breaks the code flow
            while (this->getNumberOfRunningActions() > 0){                
                    std::cout << this->getNumberOfRunningActions() << std::endl;
                    std::cout << "Waiting active action to finish" << std::endl;
            }
}

I cannot find a way to:

  1. Capture the current running sequence
  2. Wait to finish the current animation
  3. Stop the next callback
  4. Do a different animation and then add the callback to makeRandomMovement()

Maybe I’m losing the way to do something like this, any help will be appreciated

You can make delay between actions in sequence

Something, like this

Auto delayt = DelayTime::create(12);

This will delay 12 seconds til move to next action

Just design your sequence action like this for example

->. Move character----- wait. ----- jump — wait — move , and so on

Regards

1 Like

@xCode_Soul Thanks for pointing this but that’s not the problem

I have makeRandomMovement() allways running, this function randomly call animations to move left, jump etc…

The problem appears when I trigger another function (jumpToPosition), when this function is called, there is for sure some runnin action because makeRandomMovement is allways running.

I would like to avoid the next callback in the makeRandomMovement() and wait until the current action finishes to do some other animation and then continue with randomMovement

  1. Introduce a busy variable (boolean), which is set to false at the
    start
  2. Check the variable in your animation functions and only start a new animation, if it’s false
  3. Call a function in the animation sequence, which sets the variable
    to false again