Cocos2d-x Study Notes (5) two ways to play an animation with genie

Source: Internet
Author: User

Zookeeper

I have been looking at the controls over the past few days and have not figured out how to use them. There are already many examples of this on the Internet, so I will not write it. The next step is to learn about the genie class. If you learn the genie class independently, it is very easy. So I added some animation-related knowledge points and used them together to let the genie play simple frame animations.

First, we prepare the animation materials. I made a small game online and made the materials into png and plist images for the program to call. I used TexturePackerGUI to generate plist, I chose a simple standby action. Our goal is to make this picture move ~

Method 1: Use CCSpriteFrame

// Use CCTexture2D to read the image CCTexture2D * texture = CCTextureCache: sharedTextureCache ()-> addImage ("hero/hero_standby.png"); // generate the sequence frame, the four parameters (x, y, width, and height) of CCRectMake indicate that the coordinates in the image are x, and the width and height of y are width, the following figure shows the height of the CCSpriteFrame * frame0 = CCSpriteFrame: createWithTexture (texture, CCRectMake (0,114*0, 66,114 )); CCSpriteFrame * frame1 = CCSpriteFrame: createWithTexture (texture, CCRectMake (66.25, 114*0, 66,114); CCSpriteFrame * frame2 = CCSpriteFrame: createWithTexture (texture, CCRectMake (66.25*2,114*0, 66,114); CCSpriteFrame * frame3 = CCSpriteFrame: createWithTexture (texture, CCRectMake (66.25*3,114*0, 66,114 )); // The following line of code sets the default image and initial position, because COCOS2dx requires the default original image CCSprite * sprite = CCSprite: createWithSpriteFrame (frame0 ); sprite-> setPosition (ccp (size. width/2, size. height/2 + 100); addChild (sprite); // Add 5 read images to an animation sequence CCArray * animFrames = new CCArray (4 ); animFrames-> addObject (frame0); animFrames-> addObject (frame1); animFrames-> addObject (frame2); animFrames-> addObject (frame3 ); // generate the CCAnimation object 0.2f from Five animation frames to indicate the interval between each animation frames. CCAnimation * animation = CCAnimation: createWithSpriteFrames (animFrames, 0.2f ); // Finally, create an animation CCAnimate * animate = CCAnimate: create (animation) based on the animation template; // set the action sprite-> runAction (CCRepeatForever :: create (animate ));

Method 2: Use the CCSpriteFrameCache class to read plist

// Make the image you need into a plist, and then use CCSpriteFrameCache to read the CCSpriteFrameCache * cache = CCSpriteFrameCache: cached (); cache-> cached ("hero/hero_standby.plist ", "hero/hero_standby.png"); // create the genie and use the first image as the default image CCSprite * sprite1 = CCSprite :: createWithSpriteFrame (cache-> spriteFrameByName ("20005_1.png"); sprite1-> setPosition (ccp (size. width/2, size. height/2-100); addChild (sprite1); // set the animation sequence similarly. The following steps are the same as the first method: CCArray * animFrames1 = new CCArray (4 ); animFrames1-> addObject (cache-> spriteFrameByName ("20005_1.png"); animFrames1-> addObject (cache-> spriteFrameByName ("20005_3.png ")); dependencies-> addObject (cache-> dependencies ("20005_5.png"); animFrames1-> addObject (cache-> dependencies ("20005_7.png"); CCAnimation * animation1 = CCAnimation :: createWithSpriteFrames (animFrames1, 0.2f); CCAnimate * animate1 = CCAnimate: create (animation1); sprite1-> runAction (CCRepeatForever: create (animate1 ));


After carefully writing these two methods and understanding them, you can find that the first method is a bit painful and you need to locate the image yourself. I don't know if there are other more convenient methods, but obviously, the second method is more scientific. I don't know if there are other better methods? If you know it, you can talk about it ~

Some people may not know what plist is, but I will not explain it. I will check it myself. I only say what information we can use here, which records the image name and position in the big image, therefore, it is very convenient to directly read plist.

After this example, I was a little excited because I finally felt that I was playing the game.


Related Article

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.