Cocos2d-x Study Notes-Animation

Source: Internet
Author: User

This article to learn how to use the cocos2d-x engine to play Frame Animation, is to put a frame of an image as a movie.
1. First, let's take a look at the related classes.
CCAnimation: it is a parameter used by the genie to play an animation. It encapsulates a frame sequence (CCMutableArray <CCSpriteFrame *>) and the playback interval (float m_fDelay) for each frame ), when initializing this object, remember to specify the delay time; otherwise, the default value is 0.
CCAnimationCache: it is easy to see from the name that it is used to cache CCAnimation. It encapsulates a dictionary (CCMutableDictionary <std: string, CCAnimation *>) and is a singleton. Instance used:
[Cpp]
CCMutableArray <CCSpriteFrame *> * pAnimFrames = new CCMutableArray <CCSpriteFrame *> (10 );
/**....*/
CCAnimation * pAnimation = CCAnimation: animationWithFrames (pAnimFrames, 0.12f );
// Add to cache
CCAnimationCache: sharedAnimationCache ()-> addAnimation (pAnimation, "conch_animation ");
// Retrieve from Cache
CCAnimation * pAnimation2 = CCAnimationCache: sharedAnimationCache ()-> animationByName ("conch_animation ")
CCAnimate: its parent class is CCActionInterval, which is used to make animations Based on the frame sequence and interval encapsulated by CCAnimation.

The main process of playing Frame Animation in cocos2d-x is:
(1) create a CCSpriteFrame array. You can use CCSpriteFrameCache or CCTextureCache.
(2) create a CCAnimation object through the frame sequence
(3) create a CCAnimate through the CCAnimation object and interval to generate a continuous action.
(4) use the genie to execute the action

2. The following uses CCSpriteFrameCache and CCTextureCache to implement a demo of playing an animation respectively.
(1) Use CCSpriteFrameCache to obtain the animation frame. You can use the TexturePacker tool to centralize multiple scattered small images into a large image and generate a plist file. Images used in the program:

 

The following is the core code for playing an animation:

[Cpp]
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
CCSpriteFrameCache * pFrameCache = CCSpriteFrameCache: sharedSpriteFrameCache ();
PFrameCache-> addSpriteFramesWithFile (s_plistPathPropConch, s_imgPathPropConch );

M_pHero = CCSprite: spriteWithSpriteFrameName ("ani_conch_1.png ");
M_pHero-> retain ();
This-> addChild (m_pHero );
M_pHero-> setPosition (ccp (winSize. width/2, winSize. height/2 ));

Const int frameCount = 4;
CCMutableArray <CCSpriteFrame *> * pAnimFrames = new CCMutableArray <CCSpriteFrame *> (frameCount );
Char str [50] = {0 };
For (int I = 0; I! = FrameCount; ++ I)
{
Sprintf (str, "ani_conch_mongod.png", I + 1 );
CCLog ("str = % s", str );
CCSpriteFrame * pFrame = pFrameCache-> spriteFrameByName (str );
PAnimFrames-> addObject (pFrame );
}

// The default delay value is 0.
CCAnimation * pAnimation = CCAnimation: animationWithFrames (pAnimFrames, 0.12f );
PAnimFrames-> release ();
M_pHero-> runAction (CCRepeatForever: actionWithAction (CCAnimate: actionWithAnimation (pAnimation, false )));
Sprintf is used to format strings. The main function is to write formatted data into a string. Sprintf (str, “ani_conch_%d.png ", 1) then the value of str becomes: ani_conch_1.png. Other methods can be used by google or Baidu. Running the program will see a continuously rotating shell:


(2) Use CCTextureCache to obtain the animation frame. The images used in the program:

 

The core code is as follows:

[Cpp]
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

CCTexture2D * pTexture = CCTextureCache: sharedTextureCache ()-> addImage (s_imgPathKnight );
Int textureWidth = pTexture-> getContentSize (). width;
Int textureHeight = pTexture-> getContentSize (). height;
CCLog ("width = % d, height = % d", textureWidth, textureHeight );
Const int row = 4, col = 4;
Int spriteWidth = textureWidth/row, spriteHeight = textureHeight/4;

CCSpriteFrame * pHeroFrame = CCSpriteFrame: frameWithTexture (pTexture, CCRectMake (0, 0, spriteWidth, spriteHeight ));
M_pHero = CCSprite: spriteWithSpriteFrame (pHeroFrame );
M_pHero-> retain ();
This-> addChild (m_pHero );
M_pHero-> setPosition (ccp (winSize. width/2, winSize. height/2 ));

CCMutableArray <CCSpriteFrame *> * pAnimFrames = new CCMutableArray <CCSpriteFrame *> (16 );
For (int I = 0; I! = Col; ++ I)
{
CCSpriteFrame * pSpriteFrame = CCSpriteFrame: frameWithTexture (pTexture, CCRectMake (spriteWidth * I, 0, spriteWidth, spriteHeight ));
PAnimFrames-> addObject (pSpriteFrame );
}

// The default delay value is 0.
CCAnimation * pAnimation = CCAnimation: animationWithFrames (pAnimFrames, 0.2f );
PAnimFrames-> release ();
M_pHero-> runAction (CCRepeatForever: actionWithAction (CCAnimate: actionWithAnimation (pAnimation, false )));
When you run the program, you will see a samurai who keeps standing in the same place:


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.