3 ways to create an animation in a Cocos2d-x3.3beta0
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
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
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,