azamsharp
azamsharp

Reputation: 20068

What is the best way to start Cocos2d Animation of Multiple Sprites?

I have an array randomAlphabets which contains CCSprite objects. I need to start animation on these objects. The randomAlphabets array (NSMutable) can contain max of 4 elements. I am running a loop and then starting the animation. Is this the correct way?

-(void) startAnimation:(CCSprite *) sprite
{

    [self generateRandomCoordinates]; 

    id actionMove = [CCMoveTo actionWithDuration:3.0 position:ccp(x,y)];
    id actionRotate = [CCRotateBy actionWithDuration:0.0 angle:rotateBy]; 

    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(finishedMoving:)]; 

    [sprite runAction:[CCSequence actions:actionMove,actionRotate, actionMoveDone, nil]];

}

-(void) addAlphabetsOnScreen 
{
    for (int i=0; i<=randomAlphabets.count -1; i++) {

        CCSprite *sprite = [randomAlphabets objectAtIndex:i]; 

        [self generateRandomCoordinates];       

        sprite.position = ccp(x,y); 
        [self addChild:sprite]; 

        [self startAnimation:sprite]; 
    }

}

Upvotes: 0

Views: 1101

Answers (1)

Toastor
Toastor

Reputation: 8990

Sure, why not?

If have performance issues or sprites not starting their anims simultaneously, you might want to "prepare" the sequences for each sprite in one step (maybe after loading the level) and then just kick them all of in another step. 4 Sprites starting at the same time seems not too tough though.

Upvotes: 2

Related Questions