I. IntroductionOh, okay, I 've been talking about the animation Loading Method of the cocos2d-x, remember what I wrote in "cocos2d-x Study Notes 04: simple animation", a simple animation loading, you need to write more than a dozen lines of code.
- CCSize s = CCDirector: sharedDirector ()-> getWinSize ();
-
- // #1: Data class required for generating an animation
- CCTexture2D * texture = CCTextureCache: sharedTextureCache ()-> addImage ("pic2476.png ");
- CCSpriteFrame * frame0 = CCSpriteFrame: frameWithTexture (texture, CCRectMake (32*0, 48*0, 32, 48 ));
- CCSpriteFrame * frame1 = CCSpriteFrame: frameWithTexture (texture, CCRectMake (32*1, 48*0, 32, 48 ));
- CCSpriteFrame * frame2 = CCSpriteFrame: frameWithTexture (texture, CCRectMake (32*2, 48*0, 32, 48 ));
- CCSpriteFrame * frame3 = CCSpriteFrame: frameWithTexture (texture, CCRectMake (32*3, 48*0, 32, 48 ));
-
- CCMutableArray <CCSpriteFrame *> * animFrames = new CCMutableArray <CCSpriteFrame *> (4 );
- AnimFrames-> addObject (frame0 );
- AnimFrames-> addObject (frame1 );
- AnimFrames-> addObject (frame2 );
- AnimFrames-> addObject (frame3 );
-
- CCAnimation * animation = CCAnimation: animationWithFrames (animFrames, 0.2f );
- AnimFrames-> release ();
- // #2: Initialize and set Sprite
- CCSprite * sprite = CCSprite: spriteWithSpriteFrame (frame0); // you can specify an initial frame.
- Sprite-> setPosition (ccp (s. width/2, s. height/2 ));
- AddChild (sprite );
-
- // #3: Use animation to generate an animation
- CCAnimate * animate = CCAnimate: actionWithAnimation (animation, false );
- Sprite-> runAction (CCRepeatForever: actionWithAction (animate); // Replay
Well, there are a lot of loaded code, and we can simplify it by refreshing the loop. That is to say, theoretically, at least half of the total amount can be reduced.
However, there is a premise for simplifying the brush loop: the arrangement of png must be regular, otherwise it cannot be used. In addition, different actions have different frames and different delay values. Each action must be written by yourself. This is undoubtedly a very tedious process.
So I developed an animation Acker tool to solve this problem. As the name suggests, AnimatePacker is a small tool for packaging actions, allowing you to easily edit actions by yourself. This saves a lot of program coding.
Ii. Use AnimatePacker
This tool should be used with tools such as TexturePacker because they need to provide plist. In addition, to use this tool, you must first be familiar with tools such as TexturePacker and the animation code of the cocos2d-x.
Open the interface to see at a glance. People familiar with cocos2d-x programming must know what these four boxes are.
650) this. width = 650; "border =" 0 "alt =" "src =" http://img1.51cto.com/attachment/201202/144050537.png "/>
Plists: All Plist lists. Drag the plist file to the AnimatePacker window to load the file. Animations: a list of all actions. Click "camera" to create a new action, and double-click it to edit Name and Delay. SpriteFrames: the list of SpriteFrames corresponding to the current Animation. You can sort them by dragging them. Sprites: All the alternative Spirte. You can drag the Spirte to the SpriteFrames box. 650) this. width = 650; "style =" cursor: default; "alt =" "src =" file: // C: /Users/goldlion/AppData/Local/Temp/enhtmlclip/image(272.16.png "/>
Simple Steps: 1. drag the plist file to the AnimatePacker window. 2. click the camera to generate a new Animation. In the Animations box, edit Name and Delay3. drag Sprite to SpriteFrames from Sprites, and drag SpriteFrame to be sorted.
In this way, you can edit and generate them continuously. Finally, click Save to output an xml file. Here we will call him "1111. xml.
Iii. Use the parsing code
To parse 1111. xml, you need to use the following three files:
- AnimatePacker. h
- AnimatePacker. cpp
- Singleton. h // supported files to be imported
The parsing code is very simple and there are only two interfaces:
- Void AnimatePacker: loadAnimate (char * path); // load the animation in xml
- Cocos2d: CCAnimate * AnimatePacker: getAnimate (char * name); // get the animation with the specified name
Specifically, it is written as follows:
- AnimatePacker::getInstance()->loadAnimate("1111.xml");
- CCSprite *sprite=CCSprite::spriteWithSpriteFrameName("bomb_dead0.png");
- sprite->setAnchorPoint(CCPointZero);
- sprite->setPosition(ccp(size.width/2, size.height/2));
- sprite->runAction(CCRepeatForever::actionWithAction(AnimatePacker::getInstance()->getAnimate("aaa")));
- addChild(sprite,1);
AnimatePacker is written in Qt, that is, it can be written across multiple platforms. However, currently only the win32 version is available, because I have not configured the qt development environment for mac, and I will provide it later. This tool is now a beta version. You are welcome to provide bugs and suggestions for improvement. Just reply below.
This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/779533