Next section: cocos2dx-v2.3.3 Editor simple to use and different resolution adaptation
This section mainly cocos the creation and use of skeletal animation
First, the new
Create a new status bar project with the Cocos Studio tool. Such as:
Of course, you can create a new skeleton animation by creating an empty Cocos project, and then selecting the Bone animation in the file---new file, such as
Since this is only a simple usage, focus on using, all directly create heroic example animations.
Second, add animation
Select the animation mode and click the Play button below to see the default animation now.
In the example, only the all animation, we need to separate each animation by the starting end frame rate, adding the following animation:
So there is the casual animation (default), running animation (run), attack Animation (attack), injury animation (injure), death animation (death).
This is also an additional use of the skeletal animation record.
1, first add the starting frame in the right frame view
2. Directly change the position or rotation of the bones in the preview display, and the editor will automatically record the respective state of the current frame.
3, at the interval a certain frame rate and then add the corresponding end frame, and then set the coordinates and so on. This allows the animation to be played at the beginning of the end frame.
Precautions:
By default, the newly added frames will only record a few data from the left figure below, and if we need to record data like show hidden, transparency and so on, we need to tick the start recording animation option on the right before adjusting the data.
This will make a record of the other modified data, such as
Third, export to the project
Similarly, after editing a good animation, we need to follow the previous section to export to the resource Directory of the game project
For the convenience of management, we have also put the player in a separate class here.
#ifndef __cplayer_h__#define__cplayer_h__#include"cocos2d.h"#include"cocostudio/cocostudio.h"using_ns_cc;using namespaceCocostudio::timeline;enumenaction{Act_none,//NoAct_default,//StandbyAct_run,//RunningAct_attack,//AttackAct_injure,//injuredAct_death,//Death};classCplayer: Publicnode{ Public: //implement the "Static Create ()" Method manuallyCreate_func (Cplayer); Virtual BOOLinit (); voidMove (floatdeltax); voidReset (); voidAttack ();Private: voidplayaction (enaction naction); Cplayer (); ~Cplayer (); Node*M_pnode; Enaction M_nacttype; Actiontimeline*m_paction;};#endif__cplayer_h__#include"Player.h"Cplayer::cplayer (): M_pnode (null), m_paction (null), M_nacttype (Act_none) {}cplayer::~Cplayer () {M_pnode=NULL; if(m_paction) {m_paction-release (); M_paction=NULL; }}BOOLCplayer::init () {std::stringFilePath ="HERO.CSB"; M_pnode=Csloader::createnode (FilePath); if(m_pnode) {m_paction=Csloader::createtimeline (FilePath); if(m_paction) {m_paction-retain (); M_pnode-runaction (m_paction); Playaction (Enaction::act_default); } This-AddChild (M_pnode); return true; } return false;}voidCplayer::move (floatdeltax) { if(M_nacttype! = Act_default && M_nacttype! =Act_run) { return; } Size visiblesize= Director::getinstance ()getvisiblesize (); VEC2 Origin= Director::getinstance ()Getvisibleorigin ();#defineIsfloatzero (a) ((a) > -0.000001f && (a) < 0.0000001f)if(!Isfloatzero (deltax)) { floatDelta = deltax<0? -2:2; //calculate the position after the move floatDesX = This->getpositionx () +Delta; if(desx<origin.x) {DesX=origin.x; } if(Desx>origin.x +visiblesize.width) {DesX= Origin.x +Visiblesize.width; } This->setscalex (deltax<0? -1:1); This-Setpositionx (DesX); } playaction (Act_run);}voidCplayer::attack () {playaction (act_attack);}voidCplayer::reset () {playaction (enaction::act_default);}voidCplayer::P layaction (enaction naction) {if(m_paction) {if(M_nacttype = = Naction | | m_nacttype = =act_attack) { return; } M_nacttype=naction; Switch(naction) { Caseact_default:m_paction->play ("default",true); Break; Caseact_run:m_paction->play ("Run",true); Break; Caseact_attack: {m_paction->play ("Attack",false); Std::function<void() > func = [ This] () {m_paction->play ("default",true); M_nacttype=Act_default; }; M_paction-Setlastframecallfunc (func); } Break; Caseact_injure:m_paction->play ("injure",false); Break; Caseact_death:m_paction->play ("Death",false); Break; default: Break; } }}
View Code
So in the battle, after adding the corresponding header file, we can add the normal use.
// Loading player Entities M_pplayer = cplayer::create (); // Set Location 2 + origin.y)); // Add to Node This 1);
Then in the click on the event of the screen we also make the changes.
BOOLHelloworld::ontouchbegan (Touch *ptouch, Event *unused_event) { if(m_pplayer) {//prevent out -of-screenSize visiblesize = Director::getinstance ()getvisiblesize (); VEC2 Origin= Director::getinstance ()Getvisibleorigin (); if(Ptouch->getlocation (). x>visiblesize.width*0.5+origin.x) {M_pplayer-Attack (); } Else{M_pplayer->move (ptouch->Getdelta (). x); } } return true;}voidHelloworld::ontouchmoved (Touch *ptouch, Event *pevent) { if(m_pplayer) {//Click on the left half of the screenSize visiblesize = Director::getinstance ()getvisiblesize (); VEC2 Origin= Director::getinstance ()Getvisibleorigin (); if(Ptouch->getlocation (). x < visiblesize.width*0.5+origin.x) {M_pplayer->move (ptouch->Getdelta (). x); } }}voidhelloworld::ontouchended (Touch *ptouch, Event *pevent) { if(m_pplayer) {M_pplayer-Reset (); }}
This realizes the click left Half screen movement, the right half screen realizes the attack. The effect is as follows
cocos2dx-v2.3.3 Editor Skeleton animation