Search code examples
cocos2d-xsprite

How to delay an action (or) How to control program flow to wait for a particular action to complete


I have code something like this.

I have to move a sprite to particular position and after completion of that action, i want to remove it.

But it is removing it without performing the action. How, to achieve this.

sp is a Sprite

sp->runAction(MoveTo::create(1.0, Vec2(visibleSize.width/2, visibleSize.height/2)));
this->removeChild(sp);

Solution

  • auto move = MoveTo::create(1.0, Vec2(visibleSize.width/2, visibleSize.height/2));
    auto moveDone = CallFuncN( CC_CALLBACk_1(ClassName::removeSprite, this) );
    sp->runAction( Sequence::create( move, moveDone, NULL ) );
    
    //create a function removeSprite, it will be called after action move is finished
    void ClassName::removeSprite(Node* pNode) {
    
    pNode->removeFromParent();
    }