Cocos2d-x beginner's Guide (4): two ways to generate an animation of an animation Frame

Source: Internet
Author: User
Tags image png

Method 1: use multiple sets of names to display different single-image png based on a certain rule,

[Cpp]
// Create a cache
CCSpriteFrameCache * cache = CCSpriteFrameCache: sharedSpriteFrameCache ();
Char strPlist [64] = {0 };
Char strPng [64] = {0 };
Sprintf (strPlist, "fei. plist ");
// Sprintf (strPng, "fei. pvr. ccz ");
Sprintf (strPng, "fei.png ");
Cache-> addSpriteFramesWithFile (strPlist, strPng );
 
// Create each animation frame and read it from the cache.
CCMutableArray <CCSpriteFrame *> * animFrames = new CCMutableArray <CCSpriteFrame *> (6 );
 
Char str [64] = {0 };
For (int I = 1; I <= 6; I ++)
{
Sprintf (str, "Apsara 04d.png", I );
CCSpriteFrame * frame = cache-> spriteFrameByName (str );
AnimFrames-> addObject (frame );
}
 
CCAnimation * animation = CCAnimation: animationWithFrames (animFrames, 0.04f );
 
CCRepeatForever * mFly = CCRepeatForever: actionWithAction (CCAnimate: actionWithAnimation (animation, false ));
AnimFrames-> release ();
Cache-> removeSpriteFramesFromFile (strPlist );
This code is from Baidu on the Internet. Http://blog.csdn.net/yanghuiliu/article/details/6933421

Another way is to add multiple images to a joint graph, which saves more disk space.

 

[Cpp]
// Composite graph
CCTexture2D * birdTexture = CCTextureCache: sharedTextureCache ()-> addImage ("bird2.png ");
CCMutableArray <CCSpriteFrame *> * myArray = new CCMutableArray <CCSpriteFrame *> (3 );
CCSpriteFrame * myframe1 = CCSpriteFrame: frameWithTexture (birdTexture, CCRect (1,379 ));
CCSpriteFrame * myframe2 = CCSpriteFrame: frameWithTexture (birdTexture, CCRect (1,411 ));
CCSpriteFrame * myframe3 = CCSpriteFrame: frameWithTexture (birdTexture, CCRect (1,508 ));
CCSpriteFrame * myframe4 = CCSpriteFrame: frameWithTexture (birdTexture, CCRect (1,538 ));
MyArray-> addObject (myframe1); www.2cto.com
MyArray-> addObject (myframe2 );
MyArray-> addObject (myframe3 );
MyArray-> addObject (myframe4 );
CCAnimation * animation = CCAnimation: animationWithFrames (myArray, 1.0f );
CCRepeatForever * myaction = CCRepeatForever: actionWithAction (CCAnimate: actionWithAnimation (animation, false ));
CCSprite * mybird = CCSprite: spriteWithSpriteFrame (myframe1 );
This-> addChild (mybird );
Mybird-> setPosition (ccp (200,300 ));
Mybird-> runAction (myaction );
MyArray-> release ();


 

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.