[Cocos2D-X] Cocos2D-X TestCPP (4) skeleton animation in CCSkeleton Cocos2D

Source: Internet
Author: User

Cocos2D-X TestCPP (4) CCSkeleton

An example is added in Test of Cocos2d-X 2.1.3.SpineTest. In this example, the new class CCSkeleton is used to play the skeleton animation. Thank you for reminding me ). I have talked with touch L. It is said that this is needed for skeleton animation in their next cococostudio version. Let's talk about this new surprise!


CCSkeleton mainly relies on three files to play an animation.

1.2.16.png "is used to save the synthetic image of the corpse.

2. ". atlas" is used to save the information of each corpse in the synthetic graph.

3. ". JSON" is used to save action information.


Open the Resource and we will see that spineboy. atlas is the information of every corpse of the villain we see. The following uses a short piece of code to introduce its structure. The details must be confirmed only after the tool is released.


// Definitions of each parameter in the Atlas file: spineboy.png // pngformat: RGBA8888 // format RGBA8888filter: Linear, Linear // filter: Linear repeat: none // duplicate: none/** the following can contain multiple, because it is the information of a single corpse */head // body name header rotate: false // whether to rotate xy: 1,122 // start point, with the upper left corner being 0, 0 size: 121,132 // The graph size orig: 121,132 // it should be the same as the graph size offset: 0, 0 index:-1


We can see that the file header of the Atlas file contains a format: RGBA8888, which is the same as TxtruePacker, which can better save memory. The following information includes the name, start point, and size of the corpse.


Spineboy. json contains all the actions and skeleton information, so it is very large. One hop and one walk occupy 25 K of space, if you have many roles and actions in your game, you need to have a lot of resources to update when you enter the game for the first time. Okay. Open it now.Spineboy. json let's take a look at the structure in it.


Meaning of each data in JSON: {// skeleton "bones": [// first skeleton point root {"name": "root "}, // The second bone point hip his parent node is root x: 0.64 y: 114.41 {"name": "hip", "parent": "root", "x ": 0.64, "y": 114.41}, // the third left upper leg. Its parent node is hip x: 0.64 y: 114.41 rotation angle:-89.09 {"name ": "left upper leg", "parent": "hip", "length": 50.39, "x": 14.45, "y": 2.81, "rotation ": -89.09}/* the attribute here is the initial attribute of the skeleton */], // connect "slots": [// connector: left upper leg bone point Name: left upper leg connector: left-upper-leg {"name": "left upper leg", "bone": "left upper leg", "attachment ": "left-upper-leg"}], "skins": {"default": {"left upper leg": {"left-upper-leg": {"x ": 26.12, "y":-1.85, "rotation": 89.09, "width": 33, "height": 67 }}, // action "animations ": {// action name: walk "walk": {// change of bones in the time line in the walk "bones ": {// time variation of left upper leg skeleton "left upper leg": {// roate change "rotate ": [// zero angle change on the time line-26.55 {"time": 0, "angle":-26.55}, // 0.1333 angle change on the time line-8.78 {"time ": 0.1333, "angle":-8.78}], // coordinate change "translate": [// 0 X change on the time line-3 Y change-2.25 {"time ": 0, "x":-3, "y":-2.25}, // time line 0.4 X change-2.18 Y change-2.25 {"time": 0.4, "x":-2.18, "y":-2.25}] }}}


As we can see from the above annotations, there are actually two parts in JSON.

1. Information about bones, connectors, and other related bones.

2. Action information, with the change information of the action at each time point on the time line.

Careful colleagues found that the rotation and coordinates of each corpse are separated, which is also an embodiment of the author's years of experience. The benefits of doing so can make the action more flexible. Otherwise, it will be troublesome if the rotation and coordinate changes are inconsistent on the time line.


After reading the documents required for the above actions, many of you may know about them. Next let's take a look at the composition of the CCSkeleton class in the Cocos2d-X.


!!!! The newly revised blog editor cannot add 0 0 images. You can download images from the attachment.

CCSkeleton is mainly composed of Atlas and Skeleton. in Atlas, it is mainly used to save the information of the corpse texture, whileSkeleton stores the animation information.

skeletonNode = CCSkeleton::createWithFile("spine/spineboy.json","spine/spineboy.atlas");


// Parameter 1: Action parsing file parameter 2: Zombie parsing file parameter 3: Default) default scaling ratio 1

It is so easy to create a skeleton animation object. You can parse the animation file by providing it with your corpse parsing file. As for the large image of the corpse, it will find the png name of the file header in the Atlas file.


AnimationStateData_setMixByName(skeletonNode->state->data,"walk","jump",0.4f);


// Parameter 1: Action Data parameter 2: Start action name parameter 3: name of the expected action parameter 4: animation time

This function is mainly used to link two actions. For example, if you directly perform the walking action after the jump action ends, you will find that there is no link between the actions of the villain, this is a poor performance in the game. Therefore, we need to add a transition between the two actions, and its function is embodied in this.


AnimationState_setAnimationByName(skeleton->state,"walk",false);


// Parameter 1: Action parameter 2: Action name parameter 3: whether to play repeatedly

For example, when walking, we need to play the animation repeatedly. With this function, we don't have to write the code for repeated playback. Only one BOOL is needed for easy solution!


skeletonNode->timeScale = 0.3f;skeletonNode->debugBones = false;


Code 1 controls the playback speed of an animation. The default value is 1.

Code 2 is used to show the points and lines on the villain. These points and lines are the bones. When debugBones is enabled, we can clearly see the changes made to each skeleton during the animation process.


We found that there is still some code in update. Let me explain it.


skeletonNode->state->loop


This loop variable can be used to obtain whether the animation currently being executed is a loop-playing animation. It returns a Boolean value that can be used for logical judgment.


skeletonNode->state->time


He will return a float value, which will tell you that the current animation has been played several times repeatedly, taking the same path as in Test, it is very convenient to use a hop.


Well, there is no formal tool to use it. We will introduce it here first. Xiao Fei knows the power of hunger, and is ready to try to make pasta. Haha, ELE. Me, I will get dinner first. Ladies and gentlemen, 88 ~


CocoStudio discussion group: 141444261

Cocos2D-X discussion Study Group: 244959010


This article from the "Zhang Pengfei" blog, please be sure to keep this source http://502317120.blog.51cto.com/4062300/1198137

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.