cocos2d-x 2.2 CocoStudio动画和界面编辑器按钮控制以及场景编辑器使用
關(guān)于如何使用CocoStudio導出動畫文件這里就不介紹了,網(wǎng)上教程很多。這里主要介紹動畫播放的控制
1.添加獲取第幾個動畫函數(shù),看了下CCArmatureAnimation文件,系統(tǒng)沒有獲取當前第幾個動畫的接口,這里添加下,很簡單,紅色部分為添加
void CCArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, ?int loop, int tweenEasing)
{
? ? std::vector<std::string> &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<std::string> &movName = m_pAnimationData->movementNames;
CC_ASSERT((animIndex > -1) && ((unsigned int)animIndex < movName.size()));
return animIndex;
}
在.h添加成員變量:int animIndex;
2.添加獲取當前播放動畫的總幀數(shù)函數(shù)
int CCArmatureAnimation::getFrameCount()
{
return m_iDurationTween-1;
}
3.添加ui
//界面編輯器
/ ?
UILayer* uiLayer = UILayer::create(); ?
auto myLayout = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson")); ?
uiLayer->addWidget(myLayout); ?
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast<UILabelBMFont*>(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按鈕上文字的觸摸
UIButton*button = dynamic_cast<UIButton*>(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按鍵放開的回掉函數(shù)
4.添加場景編輯器:
// 加載場景資源?
CCNode *pNode = CCSSceneReader::sharedSceneReader()->createNodeWithSceneFile("Scene/FightScene.json");
if (pNode)
{
this->addChild(pNode); ?
}
// pNode 為 之前所獲取的場景資源根節(jié)點,通過此節(jié)點獲取到動畫對象,獲取方式根據(jù)在場景編輯其中設(shè)置的層次關(guān)系而定
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);
我們要實現(xiàn)的時點擊按鈕切換動畫,動畫播放到最后一幀便停止動畫,實現(xiàn)代碼如下(下面沒實現(xiàn)場景編輯器代碼):
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如下:
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<Layout*>(CCUIHELPER->createWidgetFromJsonFile("CocoStudioUI/DemoShop.ExportJson")); ?
uiLayer->addWidget(myLayout); ?
this->addChild(uiLayer);
UILabelBMFont*button_text = dynamic_cast<UILabelBMFont*>(myLayout->getChildByName("back_LabelBMFont"));
button_text->setTouchEnable(false);//屏蔽按鈕上文字的觸摸
UIButton*button = dynamic_cast<UIButton*>(myLayout->getChildByName("back_Button"));
button->addReleaseEvent(this,coco_releaseselector(HelloWorld::UI_BackButton));//添加按鍵放開的回掉函數(shù)
//動畫
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); //設(shè)置為第2個動作?
armature->getAnimation()->setAnimationInternal(0.1);//設(shè)置動畫幀時間間隔
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); //如果是第一個動作設(shè)置為第2個動作
}
else if (armature->getAnimation()->getAnimIndex() == 1)
{
armature->getAnimation()->playByIndex(0); //如果是第二個動作設(shè)置為第1個動作
}
#endif
}
void HelloWorld::UI_BackButton(CCObject *pSender)
{
CCLOG("UI_BackButtonReleased");
}
總結(jié)
以上是生活随笔為你收集整理的cocos2d-x 2.2 CocoStudio动画和界面编辑器按钮控制以及场景编辑器使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杂七杂八的杂记
- 下一篇: Webpack基础之加载器