Loop over a Callback

Hi everyone,
I have a code like :

void BDFunctions::onAnimatePlayedCard(BDCardBean* bdCardBean) {
    cocos2d::CallFunc::create(std::bind(&BDFunctions::onPathFinished, this, bdCardBean));
    auto callback = cocos2d::CallFunc::create(std::bind(&BDFunctions::onPathFinished, this, bdCardBean));
    auto moveTo = cocos2d::MoveTo::create(0.2f, cocos2d::Vec2(BDShared::getInstance()->TABLE_CARD_X, BDShared::getInstance()->TABLE_CARD_Y));
    auto seq = cocos2d::Sequence::create(moveTo, callback, nullptr);
    bdCardBean->getAnimatedSprite()->runAction(seq);
}

and this method is called inside a loop as:

for (BDCardBean* bdCardBean : bdPlayer->getCards()) {
    onAnimatePlayedCard(bdCardBean, bdPlayer);
}

But on executing this code, it seems the callback “onPathFinished” called once at the end, I don’t know what is the problem, and If somehow to join “onPathFinished” with “onAnimatePlayedCard” as one method, finishing onAnimatePlayedCard after onPathFinished,
My real problem is on Multithreading, I use gpg for RealTimeMultiplayer, but this piece of code reproduce it,

Thanks in advance,

In the above line, you are not storing the instance of the CallFunc, hence you are not doing anything to it, it will be removed in the next update call.

For this sequence, you are calling moveTo at first then you are calling your callback
so, obviously your callback actions will be called at the end for once.

There’s nothing wrong with multi-threading anywhere.
You haven’t laid the code properly I believe, please cross-check once.

Hope this helps.

Happy Coding… :smile:

Thank you for your response, I completely agree with you,
Just one thing :smiley: if you can, how to store the instance of the CallFunc, to be used for each action,

Thanks,

Hi @bendev

you are saving it anyway right… !!
using like this -

But, if you want to use the particular function again and again, then in your header file declare the variable like this -

cocos2d::CallFunc * _myCB;

Now, in your .cpp file use like this -

this->_myCB = cocos2d::CallFunc::create(YOUR_METHOD_HERE);

next time if you want to use the same callback, just clone it

example -

auto sequence = Sequence::create(moveTo, this->_myCB, nullptr); 
sprite1->runAction(sequence);

auto sequence2 = Sequence::create(this->_myCB->clone());
sprite2->runAction(sequence2);

You could also call the function seperately.

Hi @pabitrapadhy,

thank you so much for your time, I appreciate it,
I will see what I can do and then come back to you,

thanks a lot,