1
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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]
\$\endgroup\$
  • \$\begingroup\$ I have multiple sprites with different CCSequences, how would I know when all CCSequences end ? \$\endgroup\$ – tGilani Nov 1 '12 at 4:17
  • \$\begingroup\$ CCNode has a numberOfRunningActions property. Maybe you can check for each sprite when all have 0 running actions. \$\endgroup\$ – Sebastián Castro Nov 1 '12 at 11:10
0
\$\begingroup\$

You could use CCDelayTime to add a pause in the sequence

id delay = [CCDelayTime actionWithDuration:1];

Then just add this to your sequence.

\$\endgroup\$
0
\$\begingroup\$

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; 
    } 
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.