CCAction(CCAction CCFiniteTimeAction CCSpeed CCFollow)

.h

#ifndef __ACTIONS_CCACTION_H__

#define __ACTIONS_CCACTION_H__


#include "cocoa/CCObject.h"

#include "cocoa/CCGeometry.h"

#include "platform/CCPlatformMacros.h"

///

Cocos2d-x的类都放置于cocos2d命名空间下。以引擎目录下的“actions/CCAction.h”为例,我们可以看到文件的首位有两个宏:NS_CC_Begin和NS_CC_END。查看宏的定义可知,这两个宏相当于把所有的类型都包含在了cocos2d命名空间下。在游戏中,我们常使用引擎提供的另一个宏USING_NS_CC来引用cocos2d命名空间:

//

NS_CC_BEGIN


enum {

    kCCActionTagInvalid = -1,//默认 action无效的tag为  在其他地方可以通过域名::枚举值来调用

};


class CC_DLL CCAction : public CCObject 

{

public:

    CCAction(void);

    virtual ~CCAction(void); 

//取得当前动作的描述  

    const char* description();   

    //产生一个当前类的实例对象拷贝  

    virtual CCObject* copyWithZone(CCZone *pZone);


    //返回动作是否播放结束了 

    virtual bool isDone(void);


   //设置将要演示本动作的结点

    virtual void startWithTarget(CCNode *pTarget);


    virtual void stop(void);


    //每一帧传入时间流逝值,计算动作的进度

    virtual void step(float dt);


  //传入进度参数,进行相应的动作播放计算。这个参数time取0~1,比如0代表动画开始播放,0.5代表播放到一半,1代表播放到结尾。本函数是个虚拟函数,所有由CCAction派生的子类都可以重载此函数来处理自已的动作算法以达到不同的动画效果,比如移动动作,那这里可以进行移动插值算法,淡入淡出动作,可以进行淡入淡出插值算法。   

    virtual void update(float time);

    

    inline CCNode* getTarget(void) { return m_pTarget; }

   

    inline void setTarget(CCNode *pTarget) { m_pTarget = pTarget; }

    

    inline CCNode* getOriginalTarget(void) { return m_pOriginalTarget; } 

    

/** Set the original target, since target can be nil.

    Is the target that were used to run the action. Unless you are doing something complex, like CCActionManager, you should NOT call this method.

    The target is 'assigned', it is not 'retained'.

    @since v0.8.2

    */

    inline void setOriginalTarget(CCNode *pOriginalTarget) { m_pOriginalTarget = pOriginalTarget; }


    inline int getTag(void) { return m_nTag; }

    inline void setTag(int nTag) { m_nTag = nTag; }


public:


    static CCAction* create();

protected:

    CCNode    *m_pOriginalTarget;

    /** The "target".

    The target will be set with the 'startWithTarget' method.

    When the 'stop' method is called, target will be set to nil.

    The target is 'assigned', it is not 'retained'.

    */

    CCNode    *m_pTarget;

    /** The action tag. An identifier of the action */

    int     m_nTag;

};

//时间动作 CCFiniteTimeAction分为CCActionInstant(瞬时动作)CCActionInterval(延迟动作)

class CC_DLL CCFiniteTimeAction : public CCAction

{

public:

    CCFiniteTimeAction()

        : m_fDuration(0)

    {}

    virtual ~CCFiniteTimeAction(){}

//动作时间

    inline float getDuration(void) { return m_fDuration; }

    inline void setDuration(float duration) { m_fDuration = duration; }


   //反向动作

    virtual CCFiniteTimeAction* reverse(void);

protected:

 //单位秒

    float m_fDuration;

};


class CCActionInterval;

class CCRepeatForever;


/** 

 @brief Changes the speed of an action, making it take longer (speed>1)

 or less (speed<1) time.

 Useful to simulate 'slow motion' or 'fast forward' effect.

 @warning This action can't be Sequenceable because it is not an CCIntervalAction

 */

//速度动作

class CC_DLL CCSpeed : public CCAction

{

public:

    CCSpeed()

        : m_fSpeed(0.0)

        , m_pInnerAction(NULL)

    {}

    virtual ~CCSpeed(void);


    inline float getSpeed(void) { return m_fSpeed; }

    /** alter the speed of the inner function in runtime */

    inline void setSpeed(float fSpeed) { m_fSpeed = fSpeed; }


   //初始化

    bool initWithAction(CCActionInterval *pAction, float fSpeed);

//CCAction的函数重载  

    virtual CCObject* copyWithZone(CCZone *pZone);

    virtual void startWithTarget(CCNode* pTarget);

    virtual void stop();

    virtual void step(float dt);

    virtual bool isDone(void);

    virtual CCActionInterval* reverse(void);

//设置一个要控制的动作,这里称为“内部动作”,即当前动作的效果作用于另外的动作目标。

    void setInnerAction(CCActionInterval *pAction);


    inline CCActionInterval* getInnerAction()

    {

        return m_pInnerAction;

    }


public:

   

    static CCSpeed* create(CCActionInterval* pAction, float fSpeed);

protected:

    float m_fSpeed; //播放速度 

    CCActionInterval *m_pInnerAction;//控制的“内部动画” 

};


//跟随动作

class CC_DLL CCFollow : public CCAction

{

public:

    CCFollow()

        : m_pobFollowedNode(NULL)

        , m_bBoundarySet(false)

        , m_bBoundaryFullyCovered(false)        

        , m_fLeftBoundary(0.0)

        , m_fRightBoundary(0.0)

        , m_fTopBoundary(0.0)

        , m_fBottomBoundary(0.0)

    {}

    virtual ~CCFollow(void);

    

//是否有边界

    inline bool isBoundarySet(void) { return m_bBoundarySet; }


    inline void setBoudarySet(bool bValue) { m_bBoundarySet = bValue; }


   //初始化,参数1为跟随目标结点,参数2为边界矩形,默认为CCRectZero代表无边界。

    bool initWithTarget(CCNode *pFollowedNode, const CCRect& rect = CCRectZero);

  //CCAction的函数重载   

    virtual CCObject* copyWithZone(CCZone *pZone);

    virtual void step(float dt);

    virtual bool isDone(void);

    virtual void stop(void);


public:


    static CCFollow* create(CCNode *pFollowedNode, const CCRect& rect = CCRectZero);

protected:

    CCNode *m_pobFollowedNode;

//是否被限制在边界内  

    bool m_bBoundarySet;

//边界是否只是一个点,代表边界无效。

    bool m_bBoundaryFullyCovered;


  

//定义变量保存屏幕尺寸信息,是为了快速取得。 

//屏幕一半大小  

    CCPoint m_obHalfScreenSize;

//屏幕全部大小  

    CCPoint m_obFullScreenSize;


    //边界的左,右,上,下的位置。

    float m_fLeftBoundary;

    float m_fRightBoundary;

    float m_fTopBoundary;

    float m_fBottomBoundary;

};


// end of actions group

/// @}


NS_CC_END


#endif // __ACTIONS_CCACTION_H__


——————————————————————————————————————————————————————————————————————————————————————————

.cpp

  1. //取得当前动画的描述  
  2. const char* CCAction::description()  
  3. {  
  4.     //描述信息通过CCString的格式化字符串函数createWithFormat来创建字符串。  
  5.     return CCString::createWithFormat("<CCAction | Tag = %d>", m_nTag)->getCString();  
  6. }  


  1. //产生一个当前类的实例对象拷贝  
  2. CCObject* CCAction::copyWithZone(CCZone *pZone)  
  3. {  
  4.     //定义用于保存新拷贝指针的变量,参看CCZone的定义可以知道,它的功能仅仅是保存一个CCObject指针。  
  5.     CCZone *pNewZone = NULL;  
  6.     //定义用于返回结果的CCAction指针变量  
  7.     CCAction *pRet = NULL;  
  8.     //如果pZone不为空且它保存了有效的CCObject,则返回  
  9.     if (pZone && pZone->m_pCopyObject)  
  10.     {  
  11.         pRet = (CCAction*)(pZone->m_pCopyObject);  
  12.     }  
  13.     else  
  14.     {  
  15.         //否则手动创建一个新的动作实例,返回给pRet  
  16.         pRet = new CCAction();  
  17.         //这一句其实可以不加,因为后面直接释放了,没有使用。  
  18.         pNewZone = new CCZone(pRet);  
  19.     }  
  20.     //拷贝动作的用户数据  
  21.     pRet->m_nTag = m_nTag;  
  22.     //释放pNewZone。  
  23.     CC_SAFE_DELETE(pNewZone);  
  24.     return pRet;  


  1. //每一帧传入时间流逝值,计算动画的进度。在基类中并未做相应处理。需要子类进行实现。  
  2. void CCAction::step(float dt)  
  3. {  
  4.     CC_UNUSED_PARAM(dt);  
  5.     CCLOG("[Action step]. override me");  


  1. //传入进度参数,进行相应的动画播放计算。  
  2. void CCAction::update(float time)  
  3. {  
  4.     CC_UNUSED_PARAM(time);  
  5.     CCLOG("[Action update]. override me");  


  1. //每一帧传入时间流逝值,计算动画的进度。  
  2. void CCSpeed::step(float dt)  
  3. {  
  4.     //对内部动画进行相关计算,这里通过对时间流逝值的缩放实现了播放速度的调节。  
  5.     m_pInnerAction->step(dt * m_fSpeed);  
  6. }  


  1. //初始化跟随动画。  
  2. bool CCFollow::initWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)  
  3. {  
  4.     //参数跟随结点的有效性判断  
  5.     CCAssert(pFollowedNode != NULL, "");  
  6.     //因为要用到跟随节点,所以手动对其引用计数器加1。  
  7.     pFollowedNode->retain();  
  8.     //将参数跟随结点保存到变量m_pobFollowedNode。  
  9.     m_pobFollowedNode = pFollowedNode;  
  10.     //如果参数2无效,则将边界开关置为false,即无边界。  
  11.     if (rect.equals(CCRectZero))  
  12.     {  
  13.         m_bBoundarySet = false;  
  14.     }  
  15.     else  
  16.     {  
  17.         //否则有边界。  
  18.         m_bBoundarySet = true;  
  19.     }  
  20.     //假设边界是有效矩形。  
  21.     m_bBoundaryFullyCovered = false;  
  22.     //取和屏幕大小  
  23.     CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
  24.     //将屏幕大小保存到m_obFullScreenSize。  
  25.     m_obFullScreenSize = CCPointMake(winSize.width, winSize.height);  
  26.     //计算屏幕半大小保存到m_obHalfScreenSize  
  27.     m_obHalfScreenSize = ccpMult(m_obFullScreenSize, 0.5f);  
  28.     //如果有边界。将边界的四条边存入相应边量中。  
  29.     if (m_bBoundarySet)  
  30.     {  
  31.         m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x);  
  32.         m_fRightBoundary = -rect.origin.x ;  
  33.         m_fTopBoundary = -rect.origin.y;  
  34.         m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y);  
  35.         //数据错误处理:如果右边界小于左边界,则将左右边界都设为中间位置。  
  36.         if(m_fRightBoundary < m_fLeftBoundary)  
  37.         {  
  38.             m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary) / 2;  
  39.         }  
  40.         //数据错误处理:如果下边界小于上边界,则将上下边界都设为中间位置。  
  41.         if(m_fTopBoundary < m_fBottomBoundary)  
  42.         {  
  43.             m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary) / 2;  
  44.         }  
  45.         //如果左右边界位置相等且上下边界位置相等,则这个边界矩形就只是一个点。  
  46.         if( (m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary) )  
  47.         {  
  48.             m_bBoundaryFullyCovered = true;  
  49.         }  
  50.     }  
  51.       
  52.     return true;  
  53. }  













转载于:https://www.cnblogs.com/sssssnian/p/3748675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值