上回讲到如何用cocosbuilder拼接一个完整动画文件出来。
ok,这回就讲如何把动画文件导入到我们的游戏代码中并使用。
第一步:用cocosbuilder导出数据文件
很简单,选中要导出的ccb文件,然后点击file->publish就可以了。
导出的文件在你的项目目录下能找到,后缀为.ccbi的文件就是对应的ccb文件的数据导出文件了。
现在把playerAni.ccbi文件拷贝到我们vs的工程目录资源文件夹下
第二步:新建一个cocos2d-x工程
这里我以最简单的helloworld为例子就可以了
新建之后出现如下画面表示成功,这部分的内容可以去网上搜索,或者cocos2d-x官网了解
这里有个点要注意就是一定要在工程包含头文件的目录中加入 “e:\cocos2d-2.0-x-2.0.3\extensions"这个目录,e盘是我放置cocos2dx的目录,前面你们可以根据各自的具体目录具体设置。
第二步:添加ccbi文件读取类
基础的类可以从cocos2d-x的例子代码中截取,这里我给大家截取好了,3个基础动画类文件在附件中,大家可以下载使用,简单添加到工程就行了。
添加进基础文件之后,接下来,我们修改下HelloWorldScene.h和HelloWorldScene.cpp文件
首先是HelloWorldScene.h文件
头文件部分,我们要加入包含语句
- #include
"cocos-ext.h" - #include
"AnimationsLayerLoader.h" -
- using
namespace cocos2d::extension;
接下来修改HelloWorldScene.cpp文件
在HelloWorldScene.cpp的init方法中插入下面的语句
- CCBAnimationManager
*actionManager = NULL; -
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLi brary(); -
-
ccNodeLoaderLibrary->registerCCNodeLoader("AnimationsTestLayerLoade r", AnimationsTestLayerLoade r::loader()); -
-
-
cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); -
-
-
CCNode * node = ccbReader->readNodeGraphFromFile("boneCCb/playerAni.ccbi", this,&actionManager); -
-
((AnimationsTestLayer*)node)->setAnimationManager(actionManager); -
ccbReader->release(); -
-
node->setPosition(ccp(100,160)); -
-
AnimationsTestLayer * spr = (AnimationsTestLayer *)node; -
if(node != NULL) { -
this->addChild(node); -
}
今后如果要加入其它动画,只需修改
CCNode * node = ccbReader->readNodeGraphFromFile("boneCCb/playerAni.ccbi", this,&actionManager);
这句代码中的ccbi文件名就可以了
ok,大功告成,接下来运行,看看效果
- #ifndef _ANIMATIONSTESTLAYERLOADE
R_H_ - #define _ANIMATIONSTESTLAYERLOADE
R_H_ - #include "AnimationsTestLayer.h"
- class CCBReader;
- class AnimationsTestLayerLoade
r : public cocos2d::extension::CCNodeLoader{ -
public: -
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(AnimationsTestLayerLoade r, loader); -
protected: -
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(AnimationsTestLayer); - };
- #endif
- #ifndef _ANIMATIONSTESTLAYER_H_
- #define _ANIMATIONSTESTLAYER_H_
- #include "cocos2d.h"
- #include "cocos-ext.h"
- class AnimationsTestLayer
- : public cocos2d::CCNode
- //, public cocos2d::extension::CCBSelectorResolver
- //, public cocos2d::extension::CCBMemberVariableAssigne
r - {
- public:
-
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(AnimationsTestLayer,create); -
-
AnimationsTestLayer(); -
virtual ~AnimationsTestLayer(); -
-
//virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSe lector(CCObject * pTarget, cocos2d::CCString * pSelectorName); -
//virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSel ector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName); -
//virtual bool onAssignCCBMemberVariabl e(cocos2d::CCObject * pTarget, cocos2d::CCString * pMemberVariableName, cocos2d::CCNode * pNode); -
-
-
-
void setAnimationManager(cocos2d::extension::CCBAnimationManager *pAnimationManager); -
// void runActionfrom(); - private:
-
cocos2d::extension::CCBAnimationManager *mAnimationManager; - };
- #endif
- #include "AnimationsTestLayer.h"
- USING_NS_CC;
- USING_NS_CC_EXT;
- AnimationsTestLayer::AnimationsTestLayer()
- : mAnimationManager(NULL)
- {}
- AnimationsTestLayer::~AnimationsTestLayer()
- {
-
CC_SAFE_RELEASE_NULL(mAnimationManager); - }
- //SEL_MenuHandler AnimationsTestLayer::onResolveCCBCCMenuItemSe
lector(CCObject * pTarget, CCString * pSelectorName) - //{
- //
return NULL; - //}
- //SEL_CCControlHandler AnimationsTestLayer::onResolveCCBCCControlSel
ector(CCObject *pTarget, CCString*pSelectorName) { - //
- //
- //
return NULL; - //}
- //bool AnimationsTestLayer::onAssignCCBMemberVariabl
e(CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode) { - //
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAnimationManager", CCBAnimationManager *, this->mAnimationManager); - //
- //
return false; - //}
- void AnimationsTestLayer::setAnimationManager(cocos2d::extension::CCBAnimationManager *pAnimationManager)
- {
-
//CC_SAFE_RELEASE_NULL(mAnimationManager); -
mAnimationManager = pAnimationManager; -
CC_SAFE_RETAIN(mAnimationManager); - }
- //void AnimationsTestLayer::runActionfrom()
- //{
- //
mAnimationManager->runAnimations("first",3.3f); - //}
- //void AnimationsTestLayer::onCCControlButtonIdleCli
cked(CCObject *pSender, CCControlEvent pCCControlEvent) { - //
mAnimationManager->runAnimations("Idle", 0.3f); - //}
helloworld.h
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "cocos-ext.h"
- #include "AnimationsLayerLoader.h"
- #include "SimpleAudioEngine.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 recommand to return the exactly class pointer -
static cocos2d::CCScene* scene(); -
-
// a selector callback -
void menuCloseCallback(CCObject* pSender); -
// implement the "static node()" method manually -
CREATE_FUNC(HelloWorld); - };
- #endif
// __HELLOWORLD_SCENE_H__
-
// Add the sprite to HelloWorld layer as a child layer. -
this->addChild(pSprite, 0); -
bRet = true; -
} while (0); - CCBAnimationManager *actionManager = NULL;
- CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLi
brary(); - ccNodeLoaderLibrary->registerCCNodeLoader("AnimationsTestLayerLoade
r", AnimationsTestLayerLoade r::loader()); - cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary);
- CCNode * node = ccbReader->readNodeGraphFromFile("boneCCb/playerAni.ccbi", this,&actionManager);
- ((AnimationsTestLayer*)node)->setAnimationManager(actionManager);
- ccbReader->release();
- node->setPosition(ccp(100,160));
- AnimationsTestLayer * spr = (AnimationsTestLayer *)node;
- if(node != NULL) {
- this->addChild(node);
- }
- return bRet;
另外如果大家对动画方面想研究得更加深入,可以查阅火云洞红孩的博客
http://blog.youkuaiyun.com/honghaier/article/details/8075354