COCOS2D-X之骨骼动画武器换装效果Demo

我们这个Demo的效果就是点击屏幕实现武器的更换.人物换装应该是游戏中很常见的一个需求,故写此Demo以分享给需要的人.

一、我们直接在COCOS2D-X自带的HelloCpp的工程中添加代码即可.我们在初始化中添加如下代码:

 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);//开启触屏响应
 pArmature = CCArmature::create("Knight_f/Knight");
 pArmature->getAnimation()->playByIndex(0);//播放第一个动作
 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-sword4.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);
 }
二、由于我们用到了COCOS2D-X中extensions中的类,故需要加入对应的目录和相应的头文件以及lib文件

        ①、在 工程->属性->配置属性->VC++目录->包含目录中添加extensions文件夹的路径:$(SolutionDir)\extensions
        ②、添加头文件、命名空间以及涉及的库文件如下:

#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;
三、注册触屏分配器和设置其优先级
void TestUseMutiplePicture::registerWithTouchDispatcher()
{
 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, true);
}
三、响应触屏事件以实现换装.代码如下:

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
 static int nDisplayIndex  = -1;
 ++nDisplayIndex;
 nDisplayIndex = (nDisplayIndex)%5;
 pArmature->getBone("weapon")->changeDisplayByIndex(nDisplayIndex,true);
 return false;
}

四、程序运行效果截图.是不是觉得霸气外露大笑



五、补充说明:程序中涉及的骨骼动画文件下载链接http://t.cn/z86kmpg

PS:因为是基础学习,故不作过多分析.后继会有更多精彩内容,敬请大家关注害羞
本人郑重声明如下 一、本文来自CSDN博客,本文地址http://t.cn/z86s8xJ 二、All Rights Reserved. 任何个人或网站转载本文时不得移除本声明. 三、不得对文章进行修改,除非明确说明.同时欢迎大家评论转载和分享.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值