Search code examples
cocos2d-x

cocos2d-x animation play in loop


A animation move from point a to b, in the movement, the animation need to be play in loop. For example, a bullet move to a point and this bullet is a animation which should be played in loop.

CCSequence::create(
  CCSpawn::createWithTwoActions(
    CCTargetedAction::create(sprite, CCMoveTo::create(3.0f, point_a)), 
    CCTargetedAction::create(sprite, CCRepeatForever::create(CCAnimate::create(animation)))
),0);

But CCRepeatForever can not be a member of action sequence. So how to do it? I use sequence because there are others actions be queued (omitted above)


Solution

  • you don't need to use ccspawn for this.. nor ccsequence just run both actions separately on the object.

    CCSprite *newSprite=CCSprite::create("imageName");
    
    CCAnimation *animation=CCAnimation::create();
    
    //..some code to add frames to this animation object..
    
    //to repeat for indefinite time you could setLoops to -1 or use CCRepeatForever class    //like this..
    //1:
    
    animation->setLoops(-1);
    newSprite->runAction(CCAnimate:create(animation));
    
    //or..
    
    //2:
    
    newSprite->runAction(CCRepeatForever:create(CCAnimate:create(animation)));
    
    //now to translate this sprite simultaneously use this.
    
    newSprite->runAction(CCMoveTo::create(3.0,point_a));