3 ways to create an animation in Cocos2d-x3.3beta0, cocos2d Animation

Source: Internet
Author: User
Tags addchild

3 ways to create an animation in Cocos2d-x3.3beta0, cocos2d Animation
1. Independently load the genie object

This method is not recommended because rendering efficiency is low and resources are wasted. The Code is as follows: Note: you only need to paste the code to HelloWorldScene. cpp.
// First, rendering each sprite frame auto Sprite = sprite: create ("grossini_dance_01.png"); sprite-> setPosition (Vec2 (visibleSize. width/2, visibleSize. height/4*3); addChild (sprite); auto animation = Animation: create (); char strName [50] = {0}; for (int I = 1; I <= 14; I ++) {sprintf (strName, "grossini_dance_10902d.png", I); animation-> addSpriteFrameWithFile (strName ); // load all the genie to animation} animation-> setDelayPerUnit (3.0f/14); // play 14 animation animations within 3 seconds-> setRestoreOriginalFrame (true ); // replay and start playing sprite-> runAction (RepeatForever: create (Animate: create (animation); // execute the action
2. One-time loading, using the sprite Frame
Efficiency and resources are improved, but it is not convenient to use Rect to intercept genie objects. The Code is as follows:
// Second, auto textTure = ctor: getInstance ()-> getTextureCache ()-> addImage ("resize"); SpriteFrame * frame0 = SpriteFrame: createWithTexture (textTure, rect (132*0,132*0,132,200); // capture SpriteFrame * frame1 = SpriteFrame: createWithTexture (textTure, Rect (132*1,132*0,132,200 )); spriteFrame * frame2 = SpriteFrame: createWithTexture (textTure, Rect (132*2,132*0,132,200); SpriteFrame * frame3 = SpriteFrame: createWithTexture (textTure, rect (132*3,132*0,132,200); SpriteFrame * frame4 = SpriteFrame: createWithTexture (textTure, Rect (132*0,132*1,132,200); SpriteFrame * frame5 = SpriteFrame :: createWithTexture (textTure, Rect (132*1,132*1,132,200); Vector <SpriteFrame *> arr; // loads the arr of the sprite frame. pushBack (frame0); arr. pushBack (frame1); arr. pushBack (frame2); arr. pushBack (frame3); arr. pushBack (frame4); arr. pushBack (frame5); auto sp = Sprite: createWithSpriteFrame (frame0); // use the first frame of the genie object to initialize the genie sp-> setPosition (Vec2 (visibleSize. width/2, visibleSize. height/2); addChild (sp); auto animation1 = Animation: createWithSpriteFrames (arr, 0.2f); // execute the action sp-> runAction (RepeatForever: create (Animate:: create (animation1 )));
3. Use TexturePacker to package the genie object and load frames.
We recommend that you use this method: 1) Open the TexturePacker tool and import the sprite object to addSprite; 2) Select cocos2d for Data Format; 3) Use Texture format
PNG format. The Max Size W and H of Layout can be changed. But the dimension is the power of 2. 4) Publish sprite sheet. Package and save the address as the project Resource. The Code is as follows:
// A set of Third, first, and second. Use the TexturePacker tool to package the genie object in auto cache = SpriteFrameCache: getInstance (); cache-> addSpriteFramesWithFile ("animation. plist "); // load the plist File auto sp1 = Sprite: createWithSpriteFrameName (" grossini_dance_01.png "); // use the first frame of the genie to initialize the object, the name of the genie object is the same as that in plist. sp1-> setPosition (Vec2 (visibleSize. width/2, visibleSize. height/4); addChild (sp1); Vector <SpriteFrame *> arr1; char str [50] = {0}; for (int I = 1; I <= 14; I ++) {sprintf (str, "grossini_dance_10902d.png", I); // load the sprite frame to auto frame = cache-> getSpriteFrameByName (str); arr1.pushBack (frame );} // execute the auto animation2 = Animation: createWithSpriteFrames (arr1, 0.1f); sp1-> runAction (RepeatForever: create (Animate: create (animation2 )));

4,

Animation for cocos2d-x

Cocos2d motion control is flexible. In complex actions, we recommend that you separate the action from the animation and try not to use CCSpawn to bind the action. Then, you can add tags to the action to control the action. This is better.

In this case, you have an animation: AnimationStay AnimationRun AnimationAttack.
You have sports: Stay MoveTo MoveBack
Then tag these actions respectively. Then add or remove as needed. After each action or animation ends, use CCCallFuncN to control and trigger the next action, and stop an action or animation with stopActionByTag.

How to get the animation frame of the cocos2d-x

Int currentAnimIndex = 0; // The number of frames currently played by the genie for (int I = 0; I <5; I ++) {// 5 images 5 frames if (prite-> displayedFrame () = anim-> getFrames ()-> getObjectAtIndex (I )) {// This I returns only one index. If the number of frames starts from 1, + 1 currentAnimIndex = I + 1 ;}} prite-> displayedFrame () get the frame genie (CCSpriteFrame) displayed by the current genie animation ).

Anim-> getFrames () obtains the frame genie pointer array (CCSpriteFrame) Saved by the entire animation)

In fact, when we create an animation, all the image files we add will be converted to CCSpriteFrame and saved.

Animation-> getFrames ()-> getObjectAtIndex (I) obtains the element of an index in the frame spriteframe array of the animation. This element is also CCSpriteFrame.

The principle is that I use sprite-> displayedFrame () to obtain the CCSpriteFrame played by my current genie, and then compare it with my animation array (CCSpriteFrame array, obtain the subscript of the corresponding element in the array to obtain the number of frames played by the current genie.
 

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.