Cocos2dX-CCAnimation/CCAnimate

Source: Internet
Author: User

Today, we are going to learn how to create animations in cocos2dX. Some people say that we have already learned CCAction before? Why do you want to learn more? CCAction is action, and today we want to learn animation. It is to make something move.CCAnimation/CCAnimateWe know that the animation looks like it is not because the animation itself is moving, but because we switch images quickly, because our vision has a residual effect, so it looks like it's moving,


Prepare a pile of materials and make a joke. In actual development, no one will put a lot of materials there. Today we have prepared two kinds of materials. Let's take a look.

Vc + kernel + zcTcyKGz9sC0LCC2 + LK7z/G12rb + kernel/Lmkvt/KudPDtcTKxyA = "". Of course, you can also use "texturepacker, enter the subject:


Let's take a look at the usage:

CCAnimation: create ();

CCAnimation: createWithSpriteFrames (number of frames, interval between frames); // save frame information

CCAnimate: create (CCAnimation object); // Based on CCAnimation, the sprite frame is continuously switched to form an animation effect.


SetDelayPerUnit (); // sets the frame interval.

SetRestoreOriginalFrame (bool); // whether the first frame will be returned after the animation is played

SetLoops (); // number of cycles (-1 indicates an infinite loop)

SetFrames (number of frames); // sets the number of animation frames.


Before writing code, let's take a look at this configuration file:


We can see that each of our small images has a corresponding information, so we do not need to retrieve the location by ourselves.


Okay, write the code:

// Use the first image to create an genie CCSprite * fish = CCSprite: createWithSpriteFrameName ("fish01_01.png"); fish-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); addChild (fish); // create a frame number group char buff [20] = {0}; CCArray * arr = CCArray: create (); for (int I = 1; I <11; ++ I) {if (I <10) {sprintf (buff, "fish01_01_d.png", I );} else {sprintf (buff, "fish01_0000d.png", I);} CCSpriteFrame * fishFrame = CCSpriteFrameCache: sharedSpriteFrameCache ()-> spriteFrameByName (buff ); // obtain the image arr-> addObject (fishFrame) by name; // Add CCSpriteFrame to the array} // create the CCAnimation object CCAnimation * animation = CCAnimation: createWithSpriteFrames (arr, 0.2f); // set 0.2 seconds to switch between an animation and an animation-> setLoops (-1); // use CCAnimateCCAnimate * animate = CCAnimate :: create (animation); fish-> runAction (animate );



Let's take a look at the effect. Is Xiaoyu swimming? We can also join the CCAction action to make it more realistic. I will not elaborate on it here.





Next, let's take a look at how each small image is of the same size without a configuration file:

// Use CCTexture2D to read the CCTexture2D * texture = CCTextureCache: Adjust ()-> addImage ("heroHurt.png"); // retrieve the first three ccspriteframes * frame0 = CCSpriteFrame :: createWithTexture (texture, CCRectMake (400*0,240*0,400,240); CCSpriteFrame * frame1 = CCSpriteFrame: createWithTexture (texture, CCRectMake (400*1,240*0,800,240 )); CCSpriteFrame * frame2 = CCSpriteFrame: createWithTexture (texture, CCRectMake (400*2,240*0, 1200,240); // use the first one to create a genie CCSprite * sprite = CCSprite :: createWithSpriteFrame (frame0); sprite-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); addChild (sprite); CCArray * arr2 = CCArray: create (); arr2-> addObject (frame0); arr2-> addObject (frame1 ); arr2-> addObject (frame2); CCAnimation * animation2 = CCAnimation: createWithSpriteFrames (arr2, 0.2f); // set 0.2 seconds to switch one frame of image animation2-> setLoops (-1 ); // play the CCAnimate * animate2 = CCAnimate: create (animation2); sprite-> runAction (animate2 );




I remember using the following:

CCSpriteFrame * frame0 = CCSpriteFrame: frameWithTexture (texture, CCRectMake (400*0,240*0,400,240 ));

CCSprite * sprite = CCSprite: spriteWithSpriteFrame (frame0 );

I used 2.1.5 but it was not usable. I checked the official API for a long time before finding it.

CCSpriteFrame * frame0 = CCSpriteFrame: createWithTexture (texture, CCRectMake (400*0,240*0,400,240 ));

CCSprite * sprite = CCSprite: createWithSpriteFrame (frame0 );



Let's see the results:



Well, I didn't calculate the correct image location, so



So far today

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.