Question

I have created Animation for a character..need help to add this animation to existing character by its tag value.. As far I check on net..all are creating new CCSprite to make this animate..but i need it to add this animation to existing Sprite..existing Sprite's tag value is 20.. It animates well while creating new CCSprite..Thanks in Advance..

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("BearSprites.plist");
CCMutableArray<CCSpriteFrame*>* animateFrames = new CCMutableArray<CCSpriteFrame*>(9);
char str[50] = {0};
for(int i = 1; i < 9; i++)
{
    sprintf(str, "bear_frame%d.png",i);
    CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
    animateFrames->addObject(frame);
}
CCAnimation *animation = CCAnimation::animationWithFrames(animateFrames, 0.4f);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "Walk");
CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();
CCAnimation *normal = animCache->animationByName("Walk");
CCAnimate *animN = CCAnimate::actionWithAnimation(normal);
Was it helpful?

Solution

Use this code:

CCAnimate *animN = CCAnimate::actionWithAnimation(normal);
animN.tag = kTagHeroAnimation; //=272

CCSprite *sprite = (CCSprite*)[self getChildByTag:20];
[sprite stopActionByTag:kTagHeroAnimation]; //=272];
[sprite runAction: animN];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top