# Wait till all CCActions have completed

I am developing a simple cocos2d game in which I want to animate two CCSprites simultaneously, and for this purpose I simply set CCActions on respective CCSprite's as follows.

[first runAction:[CCMoveTo actionWithDuration:1 position:secondPosition]];
[second runAction:[CCMoveTo actionWithDuration:1 position:firstPosition]];


Now I want to wait till the animations are complete, so I can perform the next step. How should I wait for these animations to finish?

There are actually two method calls, the first one animates the objects via the code above and second call does the other animation.

I need to delay the second method call until the animations in first are complete. (I would not like to use CCCallFunc blocks as I want to call the second method from the same caller as the first one.

Try with a CCSequence action, here is the doc

http://www.cocos2d-iphone.org/api-ref/2.0.0/interface_c_c_sequence.html

Something like

[CCSequence actionsWithArray:first,second,finishCallFunc,nil]

• I have multiple sprites with different CCSequences, how would I know when all CCSequences end ? – tGilani Nov 1 '12 at 4:17
• CCNode has a numberOfRunningActions property. Maybe you can check for each sprite when all have 0 running actions. – Sebastián Castro Nov 1 '12 at 11:10

You could use CCDelayTime to add a pause in the sequence

id delay = [CCDelayTime actionWithDuration:1];


Have a flag if animations are running( simultaneousSpriteActionsAreRunning ). Set to true when the animations start.

In the update method of your main game loop check if each sprite has running actions using numberOfRunningActions.

so

-(void)update
{
[self checkIfSpriteActionsHaveFinished];
}

-(void)checkIfSpriteActionsHaveFinished
{
if( simultaneousSpriteActionsAreRunning
&& sprite1.numberOfRunningActions == 0
&& sprite1.numberOfRunningActions == 0 )
{
//do stuff
simultaneousSpriteActionsAreRunning = false;
}
}
`