Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the skeletal Animation in CCSkeleton Cocos2D

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand CCSkeleton Cocos2D bone animation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

CCSkeleton

A new example, SpineTest, has been added to Test in Cocos2d-X 2.1.3. SpineTest this example is mainly to introduce, a new class CCSkeleton is used to play bone animation (thank you for reminding me). I talked to touch L, and it is said that this will be used for skeletal animation in their next version of CocoStudio. All right, enough gossip. Let's introduce this new surprise.

CCSkeleton mainly depends on three files to play animation.

1. ".png" is used to save the composite image of the body block.

2. ".atlas" is used to save information about each body block in the composite image.

3. ".JSON" is used to save information about actions.

When we open the Resource, we will see that spineboy.atlas is the information about each piece of the little man we saw. Let's use a short piece of code to introduce his structure. The details will not be confirmed until the tools are released.

/ / meaning of each parameter in the Atlas file: spineboy.png / / which pngformat: RGBA8888 / / format RGBA8888filter: Linear,Linear / / filter: linear repeat: none / / repeat: none / * * there can be more than one Because it is the information of a single corpse * / head / / the name of the corpse head rotate: false / / whether the starting point of xy: 1122 / / needs to be rotated, the size of the image orig: 121,132 / / should be the same as the size of the image offset: 0j0index:-1

We can see that the header of the Atlas file contains a format:RGBA8888, which, like TxtruePacker, can better save memory. The following contains the name, starting point, size and other information of the body.

Spineboy.json contains all the action and bone information, so its volume is also very large. A jump and a walk take up 25K of space. If you have more characters and actions in the game, there will be a lot of resources that need to be updated when entering the game for the first time. Okay, let's open spineboy.json and take a look at its structure.

Meaning of each data in JSON: {/ / skeletal "bones": [/ / the first skeletal point root {"name": "root"}, / / the second skeletal point hip. His parent node is root x _ v 0.64 y _ r 114.41 {"name": "hip", "parent": "root", "x": 0.64, "y": 114.41}. / / the third skeletal point left upper leg his parent node is hip x name: "left upper leg", "parent": "hip", "length": 50.39, "x": 14.45, "y": 2.81, "rotation":-89.09} / * all the attributes here are the initial attributes of the bone * /] / / Connect "slots": [/ / Connector: left upper leg skeletal Point name: left upper leg Connector: left-upper-leg {"name": "left upper leg", "bone": "left upper leg", "p_w_upload": "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": {/ / bones changes on the timeline in walk "bones": {/ / left upper leg bone changes in time "left upper leg": {/ / roate change "rotate": [/ / 0 angle change on the timeline-26.55 {"time": 0 "angle":-26.55}, / / 0.1333 angle change on the timeline-8.78 {"time": 0.1333, "angle":-8.78}] / / change of coordinates "translate": [/ / 0 X change on timeline-3 Y change-2.25 {"time": 0, "x":-3, "y":-2.25} / / 0.4 X change on the timeline-2.18 Y change-2.25 {"time": 0.4, "x":-2.18, "y":-2.25}]}}

As we can see from the comments above, there are actually two major parts of JSON.

1. Information about bones, connectors, and so on.

two。 The action information has the change information of the action at each time point on the timeline.

Careful students will find that the data of the rotation and coordinates of each body part are separate, which is also reflected by the author's many years of experience. The benefits of this can make the movement more flexible. Otherwise, it will be troublesome if the rotation and coordinate changes are not consistent on the timeline.

After introducing the documents needed for the action above, many students already have a general understanding. Let's take a look at the composition of the class CCSkeleton in Cocos2d-X.

! The newly revised blog editor cannot add picture 0 0, please go to the attachment to download the picture.

CCSkeleton is mainly composed of Atlas and Skeleton. Atlas is mainly used to save the information of body block mapping, while Skeleton is mainly to save the animation information.

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

/ / Parameter 1: action parsing file Parameter2: body Block parsing File Parameter3: (default) scaling defaults to 1

To create a bone animation object is that simple, to provide him with your body parsing file, animation parsing file on it. As for the large picture of the body block, it will look for the png name in the Chinese header of the Atlas file.

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

/ / Parameter 1: action data parameter 2: starting action name parameter 3: action name parameter 4: connection animation time

This function is mainly used to connect two actions, for example: if you directly perform the walking action after the end of the jump, you will find that there is no convergence between the villain's actions, so the performance in the game is very bad. So we need to add a transition between two actions, and this function is reflected in this.

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

/ / Parameter 1: action parameter 2: action name parameter 3: whether to repeat playback

For example, when we walk, we want to play the animation repeatedly, and using this function, we don't have to write the code for repeated playback. Just need a BOOL to solve easily!

SkeletonNode- > timeScale = 0.3f _ debugBones _ Node-> false

Code 1 mainly controls the playback speed of the animation, and our default is 1.

Code 2 is used to show the dots and lines on the villain. These dots and lines are the bones. When debugBones is turned on, we can clearly see the changes made by each bone during the animation.

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

SkeletonNode- > state- > loop

This loop variable can get whether the currently executed animation is a circular animation, it will return a Boolean value, so that we can use to determine logically.

SkeletonNode- > state- > time

It will return a float value, which will tell you that the current animation has been played repeatedly several times, as in Test, it is very convenient to walk and jump once.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report