实习小白::cocos2d-x 2.2 study ---------- 播放动画 ( 骨骼动画 )

网上的一些内容

  


CCArmatureAnimation 如何实现在某一movement的关键帧暂停

1 动画->getAnimation()->playByIndex(1);
2 动画->getAnimation()->gotoAndPlay(2);
3 动画->getAnimation()->pause();
4 
5 1 代表第几个动作  
6 
7 2 代表你要从第几帧开始播放
8 
9 pause()要加上 不然就继续播下去了

 

cocostudio骨骼动画CCArmatureAnimation有没有帧回调呢?

1 在cocostudio设置点,然后用setFrameEventCallFunc就可以了 

 

  1 本文来自http://blog.csdn.net/runaying ,引用必须注明出处!
  2 cocos2d-X 节点(CCArmatureAnimation.h)API
  3 
  4 温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
  5 
  6 armature (衔铁)动画
  7 
  8 ///cocos2d-x-3.0alpha0/extensions/CocoStudio/Armature/animation
  9 //armature (衔铁)动画
 10 
 11 
 12 #ifndef __CCANIMATION_H__
 13 #define __CCANIMATION_H__
 14 
 15 #include "CCProcessBase.h"
 16 
 17 NS_CC_EXT_ARMATURE_BEGIN
 18 
 19 
 20 enum MovementEventType
 21 {
 22     START,
 23     COMPLETE,
 24     LOOP_COMPLETE
 25 };
 26 
 27 
 28 class Armature;
 29 class Bone;
 30 
 31 typedef void (Object::*SEL_MovementEventCallFunc)(Armature *, MovementEventType, const char *);
 32 typedef void (Object::*SEL_FrameEventCallFunc)(Bone *, const char *, int, int);
 33 
 34 #define movementEvent_selector(_SELECTOR) (SEL_MovementEventCallFunc)(&_SELECTOR)
 35 #define frameEvent_selector(_SELECTOR) (SEL_FrameEventCallFunc)(&_SELECTOR)
 36 
 37 
 38 class  ArmatureAnimation : public ProcessBase
 39 {
 40 public:
 41     /**
 42      * 使用  armature (衔铁) 创建
 43      * @param armature ArmatureAnimation 将绑定到 Armature
 44      */
 45     static ArmatureAnimation *create(Armature *armature);
 46 public:
 47     /**
 48      * @js ctor
 49      */
 50     ArmatureAnimation();
 51     /**
 52      * @js NA
 53      * @lua NA
 54      */
 55     virtual ~ArmatureAnimation(void);
 56 
 57     /**
 58      * 使用 Armature 初始化
 59      * @param armature ArmatureAnimation 将绑定到 Armature
 60      */
 61     virtual bool init(Armature *armature);
 62 
 63     /**
 64      * 动画缩放。
 65      * @param animationScale 缩放值
 66      */
 67     virtual void setAnimationScale(float animationScale);
 68     virtual float getAnimationScale() const;
 69 
 70     /**
 71      * 缩放速度
 72      * @param animationScale 缩放值
 73      */
 74     virtual void setSpeedScale(float speedScale);
 75     virtual float getSpeedScale() const;
 76 
 77     //! 动画更新速度
 78     virtual void setAnimationInternal(float animationInternal);
 79 
 80     /**
 81      * 使用动画名播放动画
 82      *
 83      * @param  animationName  你希望播放的动画名
 84      * @param  durationTo 两个动画 changing-over(变化结束)之间的帧.
 85      *         这个动画需要改变多少个帧
 86      *
 87      *         -1 : MovementData 使用这个值从动画设计面板里面获取
 88      * @param  durationTween  你想在游戏中播放的帧的数量。
 89      *        如果_durationTween是80,那么动画将在一个循环中播放80帧
 90      *
 91      *         -1 : MovementData 使用这个值从动画设计面板里面获取
 92      *
 93      * @param  loop   动画是否循环
 94      *
 95      *         loop < 0 : MovementData 使用这个值从动画设计面板里面获取
 96      *         loop = 0 : this animation is not loop
 97      *         loop > 0 : this animation is loop
 98      *
 99      * @param  tweenEasing Tween easing 用于计算慢动作效果
100      *
101      *         TWEEN_EASING_MAX : MovementData 使用这个值从动画设计面板里面获取
102      *         -1 : fade out        淡出
103      *         0  : line
104      *         1  : fade in             淡入
105      *         2  : fade in and out  淡入淡出
106      *
107      */
108     void play(const char *animationName, int durationTo = -1, int durationTween = -1,  int loop = -1, int tweenEasing = TWEEN_EASING_MAX);
109 
110     /**
111      * 通过索引播放动画,其他的参数也一样缩放
112      * @param  _animationIndex  你希望播放的动画索引
113      */
114     void playByIndex(int animationIndex,  int durationTo = -1, int durationTween = -1,  int loop = -1, int tweenEasing = TWEEN_EASING_MAX);
115 
116     /**
117      * Pause the Process(处理)        //暂停
118      */
119     virtual void pause();
120     /**
121      * Resume the Process(处理)       //恢复
122      */
123     virtual void resume();
124     /**
125      * Stop the Process(处理)         //停止
126      */
127     virtual void stop();
128 
129 
130     /**
131      * Get movement count           //获取移动次数
132      */
133     int getMovementCount();
134 
135     void update(float dt);
136 
137     /**
138      * Get current movementID               //  获取当前移动 ID
139      * @return The name of current movement     //当前 移动的名字
140      */
141     std::string getCurrentMovementID();
142 
143     /** 设置 armature (衔铁) 移动 事件的回调函数
144      * 
145      * 断开这个事件只要调用 setMovementEventCallFunc(NULL, NULL);
146      */
147     void setMovementEventCallFunc(Object *target, SEL_MovementEventCallFunc callFunc);
148 
149     /**
150      * 设置 armature (衔铁) 帧 事件的回调函数
151      * 断开这个事件只要调用 setFrameEventCallFunc(NULL, NULL);
152      */
153     void setFrameEventCallFunc(Object *target, SEL_FrameEventCallFunc callFunc);
154 
155 protected:
156 
157     /**
158      * Update(float dt) 将调用此处理程序,您可以在这里处理你的逻辑
159      */
160     void updateHandler();
161 
162     /**
163      * 更新当前关键帧,并处理自动停止,暂停
164      */
165     void updateFrameData(float currentPercent);
166 
167     /**
168      * Emit a frame event       //发出帧事件
169      */
170     void frameEvent(Bone *bone, const char *frameEventName, int originFrameIndex, int currentFrameIndex);
171 
172     friend class Tween;
173 protected:
174     //! AnimationData save all MovementDatas this animation used.       //使用这个动画的 AnimationData保存所有MovementDatas
175     CC_SYNTHESIZE_RETAIN(AnimationData *, _animationData, AnimationData);
176 
177     //! Scale the animation speed           //缩放动画速度
178     float _speedScale;
179 
180     MovementData *_movementData;                //! MovementData save all MovementFrameDatas this animation used.       //使用这个动画的 AnimationData保存所有MovementDatas
181 
182     Armature *_armature;                        //! A weak reference of armature        //一个 armature 的弱引用
183 
184     std::string _movementID;                //! Current movment's name          //当前移动的名字
185 
186     int _toIndex;                                //! The frame(帧) index in MovementData->m_pMovFrameDataArr, it's different from m_iFrameIndex.
187 
188     Array *_tweenList;
189 
190 protected:
191     /**
192      * MovementEvent CallFunc.              //移动事件回调函数
193      * @param Armature* a Armature
194      * @param MovementEventType, Event Type, like START, COMPLETE.          //事件类型 、完整
195      * @param const char*, Movement ID, also called Movement Name
196      */
197     SEL_MovementEventCallFunc _movementEventCallFunc;
198 
199     /**
200      * FrameEvent CallFunc.
201      * @param Bone*, a Bone
202      * @param const char*, the name of this frame event
203      * @param int, origin frame index
204      * @param int, current frame index, animation may be delayed (延迟)
205      */
206     SEL_FrameEventCallFunc _frameEventCallFunc;
207 
208 
209     Object *_movementEventTarget;
210     Object *_frameEventTarget;
211 };
212 
213 NS_CC_EXT_ARMATURE_END
214 
215 #endif /*__CCANIMATION_H__*/

 

 

自己代码:

 1 HelloWorld.h
 2 
 3 ......
 4 
 5 void    touchAnimationCallBack( CCObject *pSender, TouchEventType type );// 点击图片时的时候播放动画的回调函数
 6 
 7 void    movementCallFunc( CCArmature *armature, MovementEventType type, const char* evt );    // 动画的帧回调函数
 8 
 9 ......
10 
11 
12 HelloWorld.cpp
13 
14 ......
15 
16 void HelloWorld::touchAnimationCallBack( CCObject *pSender, TouchEventType type){
17 
18     //CCSprite *sprite = dynamic_cast< cocos2d::ui::UIImageView *>(pSender);
19     if ( TOUCH_EVENT_ENDED == type ){        // 在点击结束的时候添加动画
20         // 加载动画文件
21         cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager() -> addArmatureFileInfo( "animation/shuaxintexiao.ExportJson" );
22         CCArmature *armatureAnimation = cocos2d::extension::CCArmature::create("shuaxintexiao");    // 创建要播放的动画
23         //CC_DEPRECATED_ATTRIBUTE virtual void playByIndex(int animationIndex,  int durationTo = -1, int durationTween = -1,  int loop = -1, int tweenEasing = TWEEN_EASING_MAX);
24         //armatureAnimation -> getAnimation() -> playByIndex( 0 , -1, -1, 3, TWEEN_EASING_MAX);    // 通过下标选择要播放的动画
25         armatureAnimation -> getAnimation() -> playByIndex( 0 );    // 通过下标选择要播放的动画
26         //armatureAnimation -> getAnimation() -> pause();            // 暂停动画
27         armatureAnimation -> setScale( 0.5f );        // 设置其缩放大小
28         armatureAnimation -> setPosition( dynamic_cast< cocos2d::ui::UIImageView *>(pSender) -> getWorldPosition() );    // 设置播放的位置,世界坐标系
29         armatureAnimation -> getAnimation() -> setMovementEventCallFunc( this, movementEvent_selector(HelloWorld::movementCallFunc));
30         armatureAnimation -> setUserData( (void *) 10001 );     // 让其携带数据
31         this -> addChild( armatureAnimation );                                    // 将动画添加到场景中去
32     }
33 
34 }
35 
36 void HelloWorld::movementCallFunc( CCArmature *armature, MovementEventType type, const char* evt){
37     
38     if( type == COMPLETE ){
39         long num = (long)armature -> getUserData();    // 获取那个动画所携带的数据,判断是一个动画,可在这里获取外部的动画
40         armature -> removeFromParent();        // 移除动画
41         armature = NULL;
42         CCLOG("-- silent num = %d ", num );
43         CCLOG("-- silent the armature is removed --");
44     }
45 }
46 
47 ......

 

结果:

 

posted @ 2015-11-25 13:05  silent-bobo  阅读(797)  评论(0编辑  收藏  举报