Search code examples
functioncocos2d-iphoneselectorschedulerccsprite

How do I call a particular function at the end of a runAction?


I am performing a runAction on a sprite to move to a position.

[[crBalls[cb.count] getball] runAction:[CCSequence actions:[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],[CCMoveTo actionWithDuration:0.05 position:FD],nil]];

Once the sprite moves to the desired position(FD) then I want to call a function at that time. Right now I am scheduling a selector to be called after a delay of 'timeToTravel' which is the time taken by the above action to finish performing.(I am using scheduler instead of performSelector since perform selector is more prone to problems)

[self schedule:@selector(placeThatDamnBall) interval:timeToTravel+0.05];

-(void) placeThatDamnBall
{
[self unschedule:@selector(placeThatDammBall)];
[self ballPlacedIn:FD.x :FD.y :cb.type : cb.count];
}

But this is not entirely reliable and may cause problem in a rare case where the function might get called before the sprite reaches the destination. Is there any way I can avoid having to call a selector and be able to call the function once the sprite has truly reached the destination?

Thanks


Solution

  • Add a CCCallFunc at the end of your sequence:

    [[crBalls[cb.count] getball] runAction:
        [CCSequence actions:
            [CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],
            [CCMoveTo actionWithDuration:0.05 position:FD],
            [CCCallFunc actionWithTarget:self selector:@selector(placeThatDamnBall)],
            nil]];