Animation of cocos2d-x 3. x
An animation consists of frames. In the simplest case, each frame is a texture. We can use a texture sequence to create an animation. However, when drawing a video card, switching between textures is a huge overhead, because the genie can display part of the texture, therefore, it is usually more efficient to arrange the multiple textures used by the animation in a certain order and place them under the same texture. When creating an animation, We need to specify not only the texture used by the animation, but also the part of the texture used by each frame.
The cocos2d-x provides frame-class SpriteFrame to easily record texture display information. A frame contains two attributes: texture and region. Texture refers to the texture to be displayed, and area refers to the texture to be displayed. Therefore, in an animation, frame is used to represent the content of each frame.
We have prepared the frame sequence, and it is no problem to play this series of frames at a constant speed. However, we often need non-uniform animation effects, the display time of each frame requires special settings. To describe each frame, apart from frame, we also need to record the frame duration. The animation frame class AnimationFrame also contains two attributes: frame reference and Frame delay.
When there is a game that uses a lot of images on the interface, the first time you click this interface, the speed is very slow (because you want to load and draw a lot of images), we can use TextureCache to asynchronously load the texture in advance, after loading, you can enter this interface and then use these images very quickly.
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.
You can manually add sequence frames to the Animation class or use a file to initialize the Animation class.
Auto animation = Animation: create (); for (int I = 0; I <10; ++ I) {char s [100] = {0}; sprintf (s, "mongod.png", I); // Add the sprite frame to the Animation instance animation-> addSpriteFrameWithFile (s);} // set the duration of each frame, in seconds, animation-> setDelayPerUnit (2.8f/10.0f); // you can specify whether to restore the animation to the first animation frame after the animation is played. setRestoreOriginalFrame (true); auto action = Animate:: create (animation); auto sprite = Sprite: create (); sprite-> setPosition (100,100); this-> addChild (sprite); sprite-> runAction (Sequence :: create (action, action-> reverse (), NULL ));