关于如何使用CocoStudio导出动画文件这里就不介绍了,网上教程很多。这里主要介绍动画播放的控制
1.添加获取第几个动画函数,看了下CCArmatureAnimation文件,系统没有获取当前第几个动画的接口,这里添加下,很简单,红色部分为添加
void CCArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, int loop, int tweenEasing)
{
std::vector &movName = m_pAnimationData->movementNames;
CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));
animIndex = animationIndex;
std::string animationName = movName.at(animationIndex);
play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing);
}
/*获取当前播放的是第几个动画*/
int CCArmatureAnimation::getAnimIndex()
{
std::vector &movName = m_pAnimationData->movementNames;
CC_ASSERT((animIndex > -1) && ((unsigned int)animIndex < movName.size()));
return animIndex;
}
在.h添加成员变量:int animIndex;
2.添加获取当前播放动画的总帧数函数
int CCArmatureAnimation::getFrameCount()
{
return m_iDurationTween-1;
}
3.添加ui
//界面编辑器
/
UILayer* uiLayer = UILayer::create();
auto myLayout = dynamic_cast(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson"));
uiLayer->addWidget(myLayout);
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按钮上文字的触摸
UIButton*button = dynamic_cast(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按键放开的回掉函数
4.添加场景编辑器:
// 加载场景资源
CCNode *pNode = CCSSceneReader::sharedSceneReader()->createNodeWithSceneFile("Scene/FightScene.json");
if (pNode)
{
this->addChild(pNode);
}
// pNode 为 之前所获取的场景资源根节点,通过此节点获取到动画对象,获取方式根据在场景编辑其中设置的层次关系而定
CCComRender *pLoadRenderAni = (CCComRender*)(pNode->getChildByTag(10005)->getComponent("CCArmature"));
CCArmature* armLoad = (CCArmature*)(pLoadRenderAni->getNode());
// 播放动画
armLoad->getAnimation()->playByIndex(1);
//获取头像的精灵,缩放大小为2
CCComRender *pLoadRenderUI = (CCComRender*)(pNode->getChildByTag(10007)->getChildByTag(10011)->getComponent("CCSprite"));
CCSprite* headLoad = (CCSprite*)(pLoadRenderUI->getNode());
headLoad->setScale(2.0f);
我们要实现的时点击按钮切换动画,动画播放到最后一帧便停止动画,实现代码如下(下面没实现场景编辑器代码):
HelloWorld .h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
//引入扩展类
#include "cocos-ext.h"
//添加命名空间
using namespace cocos2d::extension;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
void update(float dt);
// a selector callback
void menuCloseCallback(CCObject* pSender);
//UI button selector callback
void UI_BackButton(CCObject *pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
private:
CCArmature* armature;
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp如下:
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
bool bRet = false;
do
{
//
// super init first
//
CC_BREAK_IF(! CCLayer::init());
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
//界面编辑器
/
UILayer* uiLayer = UILayer::create();
auto myLayout = dynamic_cast
(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson"));
uiLayer->addWidget(myLayout);
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按钮上文字的触摸
UIButton*button = dynamic_cast(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按键放开的回掉函数
//动画
CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("CocoStudioAnim/CaAnima.ExportJson");
armature = CCArmature::create("CaAnima");
armature->setTag(1);
CCPoint temp =armature->convertToNodeSpaceAR(ccp(100,100));
armature->setScale(0.3f);
armature->getAnimation()->playByIndex(1); //设置为第2个动作
armature->getAnimation()->setAnimationInternal(0.1);//设置动画帧时间间隔
this->addChild(armature);
armature->setPosition(300,200);
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
bRet = true;
} while (0);
this->scheduleUpdate();
return bRet;
}
void HelloWorld::update(float dt)
{
//如果动画是最后一桢,则停止动画播放
if (armature->getAnimation()->getIsPlaying()&&armature->getAnimation()->getCurrentFrameIndex()== armature->getAnimation()->getFrameCount())
{
CCLOG("update frame count is%d totleCount is%d",armature->getAnimation()->getCurrentFrameIndex(),armature->getAnimation()->getFrameCount());
armature->getAnimation()->pause();
}
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if 0
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
#else
if (armature->getAnimation()->getAnimIndex() == 0)
{
armature->getAnimation()->playByIndex(1); //如果是第一个动作设置为第2个动作
}
else if (armature->getAnimation()->getAnimIndex() == 1)
{
armature->getAnimation()->playByIndex(0); //如果是第二个动作设置为第1个动作
}
#endif
}
void HelloWorld::UI_BackButton(CCObject *pSender)
{
CCLOG("UI_BackButtonReleased");
}