One, there are two ways to implement animation frame animations in the Cocos2d-x Help document:
I'm not going to list it here, so you can look at the official documents.
http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/ V3/frame-animation/zh.md
second, there are times when we need to plist take the specified number of pictures (not all) to play the animation, next we mainly introduce this method:
1 First, we start by using Spriteframecache to create Sprite Frame Cache
Spriteframecache::getinstance ()->addspriteframeswithfile ("Test.plist");
2 , hypothesis test.plist in a Machine_1.png and the Machine_2.png these two sprite pictures, through Createwithspriteframename You can get the wizard:
Auto machine = Sprite::createwithspriteframename ("Machine_1.png");
3 , create a Vector The type of data used to hold the desired sprite frame, by for looping add animated sprites
Vector<spriteframe*>framevector;for (inti=1;i<3;i++) {char pngname[100] = {0}; sprintf (Pngname, "machine_%d.png", I); Framevector.pushback (Spriteframecache::getinstance ()->getspriteframebyname (Pngname));}
4 , set animation parameters, and play sequence frame animations
The second parameter is the duration of the animation execution Auto animation = Animation::createwithspriteframes (framevector,0.2);//sets whether to return to the original state when the animation is finished animation- >setrestoreoriginalframe (false);//Set the number of times the animation executes repeatedly animation->setloops (10000);//Initialize the animation instance with an animated cache, Play a sequence frame animation with a animate instance auto action = animate::create (animation); Machine->runaction (Sequence::create (action,action- >reverse (), NULL));
5, the complete code is as follows:
Spriteframecache::getinstance ()->addspriteframeswithfile ("test.plist");//Create Sprite machineauto machine = sprite::createwithspriteframename ("Machine_1.png"); Machine->setposition (VEC2 (VISIBLE.WIDTH/2, VISIBLE.HEIGHT/2)); This->addchild (machine,1);//Create a vector type of data to hold the desired sprite frame vector<spriteframe*> Framevector;for ( int i=1;i<3;i++) {char pngname[100] = {0};sprintf (pngName, " Machine_%d.png ", i); Spriteframe* pngnamesf = spriteframecache::getinstance ()->getspriteframebyname (pngName); Framevector.pushback (PNGNAMESF);} The second parameter is the duration of the animation execution Auto animation = animation::createwithspriteframes (framevector, 0.2);// Sets whether to return to the original state Animation->setrestoreoriginalframe (false) when the animation finishes, and//sets the number of times the animation executes repeatedly animation->setloops (10000);// Initializes a animation instance with an animated cache, plays a sequence frame animation auto action = animate::create (animation) with a animate instance;machine-> Runaction (Sequence::create (Action, action->reverse (), null));
Cocos2d-x Learning Notes (ii) several methods for the realization of sequence frame animation