Cocos2d-x Learning Notes (ii) several methods for the realization of sequence frame animation

Source: Internet
Author: User

One, there are two ways to implement animation frame animations in the Cocos2d-x Help document:

    • Manually adding sequence frames to the animation class

    • Initializing the animation class with a file

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.