1. Create the cache of the genie framework, add the corresponding animation file (plist) to it, and finally produce the animation through the cache of the animation set.
Ccspriteframecache * cache = ccspriteframecache: sharedspriteframecache (); cache-> addspriteframeswithfile ("animations/grossini. plist "); cache-> animation (" animations/images "," animations/images "); ccspritebatchnode * spritebatch = ccspritebatchnode: Create (" animations/grossini.png "); // used for batch production genie ccarray * animframes = ccarray: createwithcapacity (15); // dynamically generates arrays, similar to vecto R for (INT I = 1; I <4; I ++) {// sprintf is used for string formatting. The main function is to write formatted data into a string. Sprintf (STR, character ani_conch_character d.png ", 1) then the value of STR becomes: ani_conch_1.png sprintf (STR," grossini_blue_%02d.png ", I); // two digits. If not, complete ccspriteframe * frame = cache-> spriteframebyname (STR); animframes-> addobject (FRAME);} animation = ccanimation: createwithspriteframes (animframes, 0.2f ); // create an animation set. The switching time is 0.2 S. // Add an animation to the cache ccanimationcache: sharedanimationcache ()-> addanimation (animation, "dance_blue "); // Add the animation set to the animation set cache and name it dance_blue ccanimationcache * animcache = ccanimationcache: sharedanimationcache (); // share the animation set cache ccanimation * normal = animcache-> animationbyname ("dance"); // obtain the animation set cache ccanimate * animn = ccanimate: Create (normal ); // create an animation
In addition, ccstring * STR = ccstring: createwithformat ("market_chipslogo#d.png", idx); can also be used to replace numbers.
2. directly input multiple image files to generate an animation
Ccsprite * mainsprite = ccsprite: Create ("catbody1.png"); ccanimation * animation = ccanimation: Create (); animation-> addspriteframewithfilename ("catbody1.png "); animation-> animation ("catBody2-4.png"); animation-> addspriteframewithfilename ("catbody3.png"); animation-> animation ("catBody2-4.png"); animation-> setdelayperunit (0.1f ); // set the animation interval animation-> setrestoreoriginalframe (true); // whether to return the first frame mainsprite-> runaction (ccrepeatforever: Create (ccanimate :: create (animation )));
3. directly input a large image that contains multiple small images to generate an animation.
CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png"); CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32)); CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32)); CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(64,0,32,32)); CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32)); CCArray *animFrames=CCArray::create(); CC_BREAK_IF(!animFrames); animFrames->addObject(frame0); animFrames->addObject(frame1); animFrames->addObject(frame2); animFrames->addObject(frame3); CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f); heroSprite0->runAction(CCRepeatForever::create(animate));
Cocos2d-x create sprite Animation Mode Summary