Search code examples
c++objective-ccocos2d-xcocos2d-android

Animation using .png files cocos2dx


I have 34 .png image files for 9 different scenarios. Each time the user picks 1 of those 9 scenarios and according to that I generate animation using the following

 std::string name;
 MixingScreen::animation = CCAnimation::create();
 // load image file from local file system to CCSpriteFrame, then add into CCAnimation
 for (int i = 0; i < 34; i++)
 {
     std::stringstream st,ii;
     st << Constants::flav;
     if (i<10)
     {
         ii<<i;
        name="screen/screen02/fx/sugar/0" + st.str()+"/a_0000"+ii.str()+".png";
     }
     else
     {
         ii<<i;
        name="screen/screen02/fx/sugar/0" + st.str()+"/a_000"+ii.str()+".png";
     }
        //sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i);
     MixingScreen::animation->addSpriteFrameWithFileName(name.c_str()); 
     }
     MixingScreen::animation->setDelayPerUnit(5.0f / 34.0f);

    action = CCAnimate::create(MixingScreen::animation);
    CCCallFunc *done = CCCallFunc::create(this, callfunc_selector(MixingScreen::doneMixing));
    CCSequence *readySequence = CCSequence::create(action,done,NULL);
    particles->runAction(readySequence);  

The problem I am facing is that when this animation runs, there is a time lag(everything stops for few seconds) and then the animation starts. Any solution?


Solution

  • Every time you call animation->addSpriteFrameWithFileName() a new "CCSpriteFrame" is created.

    Instead you should first add the sprite frames to CCSpriteFrameCache and use

    animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameWithName(""))
    

    Ps: these function names might be a little different but i hope you get the idea.