Introduction
In the Cocos2d-x, the specific content of the animation is displayed by the genie, In order to Display Dynamic Pictures, we need to constantly switch the sprite display content, you can convert the static genie into an animated player to achieve the animation effect. An animation consists of frames. Each frame is a texture. We can use a texture sequence to create an animation.
We use the Animation class to describe an Animation, while the sprite displays the Animation action as an Animation object. An animation is an animation action that is created by an animation object and executed by the genie.
Creation Method
-Manually add sequence frames to the Animation class
-Use a file to initialize the Animation class
Manually add
Manually add the genie to be displayed for each frame to the Animation class in sequence, and set the playback time for each frame so that the Animation can be played at a constant speed. In addition, you need to use 'setstoreoriginalframe 'to determine whether to restore the animation to the first frame after it is played. After creating an Animation instance, you must create an Animation instance to play the sequence frame Animation.
Copy code
- Auto animation = Animation: create ();
- For (int I = 1; I <15; I ++)
- {
- Char szName [100] = {0 };
- Sprintf (szName, "Images/grossini_dance_10902d.png", I );
- Animation-> addSpriteFrameWithFile (szName );
- }
- // Shocould last 2.8 seconds. And there are 14 frames.
- Animation-> setDelayPerUnit (2.8f/14.0f );
- Animation-> setRestoreOriginalFrame (true );
- Auto action = Animate: create (animation );
- _ Grossini-> runAction (Sequence: create (action, action-> reverse (), NULL ));
|
The following interfaces are used to create an Animation instance:
-'Addspriteframework': add the sprite frame to the Animation instance.
-'Setdelayunits ': sets the duration of each frame, in seconds.
-'Setstoreoriginalframework': Specifies whether to restore the animation to the first frame after it is played.
-'Clone ': clone an Animation instance.
File Addition
First, let's take a look at the expected AnimationCache class. The AnimationCache can load xml/plist files. The plist files store information about the animations and obtain the animations in the plist files through this class.
When using the AnimationCache class, the following interfaces are used:
-'Addanimationswithfile': add the animation file to the cache and plist the file.
-'Getanimation ': obtains the animation object from the cache.
-'Getinstance': obtains the animation cache instance object.
To add a file, you only need to add the created plist file to the animation cache. The plist file contains information about sequence frames. Use the Animation cache to initialize the Animation instance, and use the Animation instance to play the sequence frame Animation.
Copy code
- Auto cache = AnimationCache: getInstance ();
- Cache-> addAnimationsWithFile ("animation/animations-2.plist ");
- Auto animation2 = cache-> getAnimation ("dance_1 ");
- Auto action2 = Animate: create (animation2 );
- _ Tamara-> runAction (Sequence: create (action2, action2-> reverse (), NULL ));
|
'Note: 'starts with 3. 0, and the Cocos2d-x uses getInstance to get the singleton instance.
Animation Cache)
Generally, for a Sprite animation, You need to load the sprite frame each time you create it, add it to the array in order, and then create the corresponding sequence class. This is a very cumbersome computing process. For animations with high frequency, such as walking animations, adding them to the cache can effectively reduce the huge consumption of each creation. Because the purpose and cache content of this class are very simple and direct, its interface is also the simplest, as shown below:
-Static AnimationCache * getInstance (): globally shared Singleton
-Void addAnimation (Animation * animation, const std: string & name): Add an Animation to the cache
-Void addAnimationsWithFile (const std: string & plist) to add the animation file to the cache.
-Void removeAnimation (const std: string & name): removes a specified animation.
-Animation * getAnimation (const std: string & name) to obtain the saved Animation.
'Suggestion: 'Add the following cache cleanup operations to the memory warning:
Copy code
- Void releaseCaches ()
- {
- AnimationCache: destroyInstance ();
- SpriteFrameCache: getInstance ()-> removeUnusedSpriteFrames ();
- TextureCache: getInstance ()-> removeUnuserdTextures ();
- }
|
It is worth noting that the order of cleanup, we recommend that you first clean up the animation cache, then clean up the fine [frame cache] (https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/v3/spriteframe-cache/zh.md), and finally the [texture cache] (https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/v3/texture-cache/zh.md ). The reference level ranges from high to low to ensure that the release of the reference is valid.