The effect of our demo is to click on the screen to achieve the replacement of weapons. Character Dressup should be a very common requirement in the game, so write this demo to share it with the people who need it.
First, we directly in the Cocos2d-x bring the Hellocpp project to add code can. We add the following code to the initialization:
Ccsize Szwin = Ccdirector::shareddirector ()->getvisiblesize (); Ccarmaturedatamanager::sharedarmaturedatamanager ()->addarmaturefileinfo ("Knight.png", "Knight.plist", "
Knight.xml "); Ccarmaturedatamanager::sharedarmaturedatamanager ()->addarmaturefileinfo ("Weapon.png", "Weapon.plist", "
Weapon.xml ");
Settouchenabled (TRUE);//Turn on touch screen response parmature = ccarmature::create ("Knight_f/knight"); Parmature->getanimation ()->playbyindex (0);//Play The First Action Parmature->setposition (CCP (szwin.width/2-100,
SZWIN.HEIGHT/2));
Parmature->setscale (1.5f);
This->addchild (parmature); std::string sweaponname[] = {"Weapon_f-sword.png", "Weapon_f-sword2.png", "Weapon_f-sword3.png", "Weapon_f-sword
4.png "," Weapon_f-sword5.png "};
Ccspritedisplaydata Sprdisplaydata; for (int i=0;i<sizeof (sweaponname)/sizeof (sweaponname[0]), i++) {Sprdisplaydata.setparam (Sweaponname[i].c_str ()
);
Parmature->getbone ("Weapon")->adddisplay (&sprdisplaydata,i); }
Second, because we use the cocos2d-x in the extensions class, it is necessary to add the corresponding directory and the corresponding header file and Lib file
①, the path to add Extensions folder in the Include directory, configuration Properties, properties---->vc++ directory: $ (solutiondir) \extensions
②, add header files, namespaces, and the library files involved are as follows:
#include "cocos-ext.h"
#include "ccarmature/utils/ccarmaturedatamanager.h"
#include "ccarmature/ CCArmature.h "
#pragma comment (lib," LibBox2d.lib ")
#pragma comment (lib," LibExtensions.lib ")
using namespace extension;
Third, register the touch screen dispenser and set its priority
void Testusemutiplepicture::registerwithtouchdispatcher ()
{
ccdirector::shareddirector () Gettouchdispatcher ()->addtargeteddelegate (this,0, True);
}
Third, respond to touch screen events to achieve the facelift. The code is as follows:
BOOL Helloworld::cctouchbegan (Cctouch *ptouch, ccevent *pevent)
{
static int ndisplayindex =-1;
++ndisplayindex;
Ndisplayindex = (ndisplayindex)%5;
Parmature->getbone ("Weapon")->changedisplaybyindex (ndisplayindex,true);
return false;
}
Four, the program operation effect screenshot. is not feel domineering exposed
V. Supplementary NOTE: Skeletal animation files involved in the program download link http://t.cn/z86kmpg
PS: Because it is basic learning, it does not make too much analysis. Follow-up will have more exciting content, please pay attention to