Cocos2d-x Study notes (children under five) sprites play animations in two ways

Source: Internet
Author: User
Tags addchild



These days in the view of the control class, temporarily did not want to practical use of the direction. The simple creation of the online already has a lot of this kind of example, I will not write. Next is to learn the Elf class. Elf classes are also very easy if they are taught alone. So I added some knowledge about animation to use with the Elf class. Let the sprite play a simple frame animation.

First we prepare the animated material, I have a small game on the net. The material inside is made into png and plist for the program to call. I use Texturepackergui to generate plist . I chose a simple standby action, and our aim is to get this picture moving.

The first way: using ccspriteframe

Use cctexture2d to read pictures cctexture2d *texture = Cctexturecache::sharedtexturecache ()->addimage ("Hero/hero_ Standby.png ");//Generate sequence frames. The 4-Ccrectmake (X,y,width,height) represents the image with the coordinates of X, Y, and width,height in the picture//the following read 4 diagram is temporarily present in frame 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 is to set the default diagram Film and initial position due to COCOS2DX need default original picture ccsprite* sprite = Ccsprite::createwithspriteframe (FRAME0); Sprite->setposition (CCP ( SIZE.WIDTH/2, SIZE.HEIGHT/2 +100)); AddChild (sprite);//Add 5 read-out graphs to an animation sequence Ccarray *animframes = new Ccarray (4); Animframes->addobject (FRAME0); Animframes->addobject (frame1); Animframes->addobject (frame2); animframes- >addobject (Frame3);//Will be 5Animation frame Generation Ccanimation Object 0.2f represents the interval between each ccanimation *animation = Ccanimation::createwithspriteframes (Animframes, 0.2f); /finally create an animation based on the animation template ccanimate *animate = ccanimate::create (animation);//Set the wizard to create a good action sprite->runaction ( Ccrepeatforever::create (animate));

Another way: read plist using the Ccspriteframecache class

Make the picture you need plist, then read it with Ccspriteframecache ccspriteframecache* cache = Ccspriteframecache::sharedspriteframecache ( ); Cache->addspriteframeswithfile ("Hero/hero_standby.plist", "hero/hero_standby.png");// Create the Sprite and read the first picture as the default picture ccsprite* sprite1 = Ccsprite::createwithspriteframe (Cache->spriteframebyname ("20005_1. PNG ")), Sprite1->setposition (CCP (SIZE.WIDTH/2, SIZE.HEIGHT/2)), AddChild (sprite1);//The same set animation sequence. The following steps are the same as in the first way ccarray *animframes1 = new Ccarray (4); Animframes1->addobject (Cache->spriteframebyname ("20005_ 1.png ")); Animframes1->addobject (Cache->spriteframebyname (" 20005_3.png ")); Animframes1->addobject ( Cache->spriteframebyname ("20005_5.png")); Animframes1->addobject (Cache->spriteframebyname ("20005_7.png ")); Ccanimation *animation1 = Ccanimation::createwithspriteframes (animFrames1, 0.2f); Ccanimate *animate1 = ccanimate::create (Animation1); Sprite1->runaction (Ccrepeatforever::create (animate1));


Carefully written in these two ways and understand that you can find that the first way some eggs ache, need to find their own picture location, I do not know if there are other more convenient way, but it is obviously another way more scientific. I wonder if there's any better way? Know the classmate can exchange for a moment oh ~

Someone may not know what plist is. I won't explain it in detail. To check, I just said that we can use the information, which recorded the name of the picture and the location in the large map, so directly read plist is very convenient.

It's still a little exciting to finish this example. Because finally a bit think oneself is playing the game, hey.


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Cocos2d-x Study notes (children under five) sprites play animations in two ways

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.