Cocos2dx animation Animation

Source: Internet
Author: User
Frame Animation 1. You can create an animation through a series of image files, such as the following: [CPP] ccanimation * animation = ccanimation: Create (); // load the image file from the local file system to the ccspriteframe area and add it to ccanimation for (INT I = 1; I <15; I ++) {char szimagefilename [128] = {0}; sprintf (szimagefilename, "images/grossini_dance_10902d.png", I); animation-> addspriteframewithfilename (szimagefilename );} animation-> setdelayperunit (2.8f/14.0f); // The animation consists of 14 frames, which will last for 2.8 seconds. animation-> Setrestoreoriginalframe (true); // after 14 frames are played, return to the first frame ccanimate * Action = ccanimate: Create (animation); sprite-> runaction (action ); // execute the sprite object. Note that ccanimation consists of many sprite frames. It can set the interval and duration. It actually includes a set of data. Ccanimate is an action created through a ccanimation object. 2. Although manual animation is easy to understand, it is rarely used in game development. On the contrary, Sprite table animation is often used in 2D animation. This is a genie table. It is actually a series of animated frame images, or an image set that can be used in a scenario. In opengles1.1, the following table is widely used: 1. Reduce the file read/write time. Reading an image is faster than reading a pile of small files. 2. Reduce memory consumption. OpenGL ES 1.1 can only use an image of the power of 2 (that is, the width or height is 864,128,256,512,102, 4 ,...). That is to say, OpenGL es1.1 will allocate the several power of memory space to each image 2, even if this image cannot reach the width and height of the image, it will be allocated a space larger than the n size of the image. Using this image set will reduce memory fragments. 3. Reduce OpenGL ES rendering calls and accelerate rendering. Cocos2d-x V2.0 upgraded to OpenGL es2.0.opengl es2.0 won't reallocate the power of 2 memory blocks, but reduce the read time and draw the advantage of calling still exist. What is the animation effect? As we can see, a Sprite table is not a required condition for animation. However, considering some of the above advantages, the genie table is very efficient. There are many different ways to create a genie table in cocos2dx. In cocos2dx 0. X and 1. x versions, ccspritesheet is designed for the above purposes. In V2.0, ccspritebatchnode replaces ccspritesheet. The ccspritebatchnode object includes the Image Textures of all Sprite frames. Even if it is not drawn, you must add it to the scene, for example: [CPP] ccspritebatchnode * spritebatch = ccspritebatchnode: Create ("animations/grossini.png"); next, you need to use the ccspriteframecache instance to ensure the corresponding frame boundary of the frame name. That is, the area in which the image is located. For example: [CPP] ccspriteframecache * cache = ccspriteframecache: sharedspriteframecache (); cache-> addspriteframeswithfile ("animations/grossini. plist "); once your genie table and frame are loaded and the genie table has been added to the scenario, you can use the createwithspriteframename method to create the genie. Addchild must be added to the sprite table: [CPP] m_psprite1 = ccsprite: createwithspriteframename ("batch"); spritebatch-> addchild (m_psprite1); addchild (spritebatch ); createwithspriteframename will find the desired image from grossini.plistand cut the texture of grossini.png into a Sprite frame. Now we create a ccarray object and all the frames of the tianjian are animated. In this animation example, we found that all 14 frames have the same size, so we can use a nested loop to traverse them, in addition, after 14 frames are added, the cycle will end. [CPP] ccarray * animframes = ccarray: createwithcapacity (15); char STR [100] = {0}; For (INT I = 1; I <15; I ++) {sprintf (STR, "grossini_dance_10902d.png", I); ccspriteframe * frame = cache-> spriteframebyname (STR); animframes-> addobject (FRAME);} finally, we need to create a ccanimate action instance to execute ccsprite. Here we can wrap the ccanimate action in the ccrepeatforever action to keep it running, like this: [CPP] ccanimation * animation = ccanimation: createwithspriteframes (animframes, 0.3f ); m_psprite1-> runaction (ccrepeatforever: Create (ccanimate: Create (animation); 4. File animation ccanimatecache can load an XML/plist file describing a batch of nodes, contains the frame name and their rectangular area. This excuse is easy to use. [CPP] ccanimationcache * cache = ccanimationcache: sharedanimationcache (); // cache in cocos2dx is a singleton mode cache-> addanimationswithfile ("animations/animations-2.plist "); ccanimation animation = cache-> animationbyname ("dance_1"); ccanimate animate = ccanimate: Create (animation); sprite-> runaction (animate );

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.