CCActionEase(可以对动作进行包装 使动作的in和out速度发生改变 使动作更平滑)

2024-01-30 21:38

本文主要是介绍CCActionEase(可以对动作进行包装 使动作的in和out速度发生改变 使动作更平滑),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

详细在cocos2dx->CCActionEase想说爱你也不难

#ifndef __ACTION_CCEASE_ACTION_H__

#define __ACTION_CCEASE_ACTION_H__


#include "CCActionInterval.h"


NS_CC_BEGIN


class CCObject;

class CCZone;


class CC_DLL CCActionEase : public CCActionInterval

//ease 减轻 缓和 使安心 这个类是所有ease action的基类

{

public:

    virtual ~CCActionEase(void);


    bool initWithAction(CCActionInterval *pAction);


    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual void startWithTarget(CCNode *pTarget);

    virtual void stop(void);

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);//反转动作

    virtual CCActionInterval* getInnerAction();//得到内部action 未进行速度调整的原始动作


public:


    /** creates the action */

    static CCActionEase* create(CCActionInterval *pAction); 

//通过原始动作创建ease action


protected:

    CCActionInterval *m_pInner;//内部action

};


/** 

 @brief Base class for Easing actions with rate(比率 速度) parameters(参数)

 */

class CC_DLL CCEaseRateAction : public CCActionEase

 //多出一个rate参数 仍旧是基类

{

public:

    virtual ~CCEaseRateAction(void);


    inline void setRate(float rate) { m_fRate = rate; }


    inline float getRate(void) { return m_fRate; }


    /** Initializes the action with the inner action and the rate parameter */

    bool initWithAction(CCActionInterval *pAction, float fRate);


    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse(void);


public:


    /** Creates the action with the inner action and the rate parameter */

    static CCEaseRateAction* create(CCActionInterval* pAction, float fRate);


protected:

    float m_fRate;

};


/** 

 @brief CCEaseIn action with a rate

 */

class CC_DLL CCEaseIn : public CCEaseRateAction

{

public:

    virtual void update(float time);//

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);

public:


    /** Creates the action with the inner action and the rate parameter */

    static CCEaseIn* create(CCActionInterval* pAction, float fRate);

};


/** 

 @brief CCEaseOut action with a rate

 */

class CC_DLL CCEaseOut : public CCEaseRateAction

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse();

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** Creates the action with the inner action and the rate parameter */

    static CCEaseOut* create(CCActionInterval* pAction, float fRate);

};


/** 

 @brief CCEaseInOut action with a rate

 @ingroup Actions

 */

class CC_DLL CCEaseInOut : public CCEaseRateAction

{

public:

    virtual void update(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse(void);


public:


    /** Creates the action with the inner action and the rate parameter */

    static CCEaseInOut* create(CCActionInterval* pAction, float fRate);

};


/** 

 @brief CCEase Exponential In

 @ingroup Actions

 */

class CC_DLL CCEaseExponentialIn : public CCActionEase  //Exponential 指数

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:

    /** creates the action */

    static CCEaseExponentialIn* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Exponential Out

 @ingroup Actions

 */

class CC_DLL CCEaseExponentialOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:

    /** creates the action */

    static CCEaseExponentialOut* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Exponential InOut

 @ingroup Actions

 */

class CC_DLL CCEaseExponentialInOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse();


public:


    /** creates the action */

    static CCEaseExponentialInOut* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Sine In

 @ingroup Actions

 */

class CC_DLL CCEaseSineIn : public CCActionEase//sine 正弦

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:

    /** creates the action */

    static CCEaseSineIn* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Sine Out

 @ingroup Actions

 */

class CC_DLL CCEaseSineOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** creates the action */

    static CCEaseSineOut* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Sine InOut

 @ingroup Actions

 */

class CC_DLL CCEaseSineInOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse();


public:


    /** creates the action */

    static CCEaseSineInOut* create(CCActionInterval* pAction);

};


/** 

 @brief Ease Elastic abstract class

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseElastic : public CCActionEase  //CCEaseElastic 有弹性的;灵活的;易伸缩的

{

public:

    /** get period of the wave in radians. default is 0.3 */

    inline float getPeriod(void) { return m_fPeriod; }   // Period 周期

    /** set period of the wave in radians. */

    inline void setPeriod(float fPeriod) { m_fPeriod = fPeriod; }


    /** Initializes the action with the inner action and the period in radians(弧度) (default is 0.3) */

    bool initWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);


    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** Creates the action with the inner action and the period in radians (default is 0.3) */

    static CCEaseElastic* create(CCActionInterval *pAction, float fPeriod);

    static CCEaseElastic* create(CCActionInterval *pAction);

protected:

    float m_fPeriod;

};


/** 

 @brief Ease Elastic In action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseElasticIn : public CCEaseElastic

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** Creates the action with the inner action and the period in radians (default is 0.3) */

    static CCEaseElasticIn* create(CCActionInterval *pAction, float fPeriod);

    static CCEaseElasticIn* create(CCActionInterval *pAction);

};


/** 

 @brief Ease Elastic Out action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseElasticOut : public CCEaseElastic

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** Creates the action with the inner action and the period in radians (default is 0.3) */

    static CCEaseElasticOut* create(CCActionInterval *pAction, float fPeriod);

    static CCEaseElasticOut* create(CCActionInterval *pAction);

};


/** 

 @brief Ease Elastic InOut action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseElasticInOut : public CCEaseElastic

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** Creates the action with the inner action and the period in radians (default is 0.3) */

    static CCEaseElasticInOut* create(CCActionInterval *pAction, float fPeriod);

    static CCEaseElasticInOut* create(CCActionInterval *pAction);

};


/** 

 @brief CCEaseBounce abstract class.

 @since v0.8.2

 @ingroup Actions

*/

class CC_DLL CCEaseBounce : public CCActionEase//Bounce 弹跳

{

public:

    float bounceTime(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse();


public:


    /** creates the action */

    static CCEaseBounce* create(CCActionInterval* pAction);

};


/** 

 @brief CCEaseBounceIn action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

*/

class CC_DLL CCEaseBounceIn : public CCEaseBounce

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** creates the action */

    static CCEaseBounceIn* create(CCActionInterval* pAction);

};


/** 

 @brief EaseBounceOut action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseBounceOut : public CCEaseBounce

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** creates the action */

    static CCEaseBounceOut* create(CCActionInterval* pAction);

};


/** 

 @brief CCEaseBounceInOut action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseBounceInOut : public CCEaseBounce

{

public:

    virtual void update(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse();


public:


    /** creates the action */

    static CCEaseBounceInOut* create(CCActionInterval* pAction);

};


/** 

 @brief CCEaseBackIn action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseBackIn : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** creates the action */

    static CCEaseBackIn* create(CCActionInterval* pAction);

};


/** 

 @brief CCEaseBackOut action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseBackOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCActionInterval* reverse(void);

    virtual CCObject* copyWithZone(CCZone* pZone);


public:


    /** creates the action */

    static CCEaseBackOut* create(CCActionInterval* pAction);

};


/** 

 @brief CCEaseBackInOut action.

 @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action.

 @since v0.8.2

 @ingroup Actions

 */

class CC_DLL CCEaseBackInOut : public CCActionEase

{

public:

    virtual void update(float time);

    virtual CCObject* copyWithZone(CCZone* pZone);

    virtual CCActionInterval* reverse();


public:


    /** creates the action */

    static CCEaseBackInOut* create(CCActionInterval* pAction);

};


// end of actions group

/// @}


NS_CC_END


#endif // __ACTION_CCEASE_ACTION_H__

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.cpp

void CCEaseIn::update(float time)

{

    m_pInner->update(powf(time, m_fRate));

}

//

Standard C 语言标准函数库


powf 函数说明

功能与pow一致,只是输入与输出皆为浮点数  


原型:extern float pow(float x, float y);
  
  用法:#include <math.h>
  
  功能:计算x的y次幂。
  
  说明:x应大于零,返回幂指数的结果。

//


CCActionInterval* CCEaseIn::reverse(void)

{

    return CCEaseIn::create(m_pInner->reverse(), 1 / m_fRate);

}


void CCEaseOut::update(float time)

{

    m_pInner->update(powf(time, 1 / m_fRate));

}


CCActionInterval* CCEaseOut::reverse()

{

    return CCEaseOut::create(m_pInner->reverse(), 1 / m_fRate);

}



void CCEaseInOut::update(float time)

{

    time *= 2;

    if (time < 1)

    {

        m_pInner->update(0.5f * powf(time, m_fRate));

    }

    else

    {

        m_pInner->update(1.0f - 0.5f * powf(2-time, m_fRate));

    }

}


// InOut and OutIn are symmetrical

CCActionInterval* CCEaseInOut::reverse(void)

{

    return CCEaseInOut::create(m_pInner->reverse(), m_fRate);

}


void CCEaseExponentialIn::update(float time)

{

    m_pInner->update(time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f);

}


CCActionInterval* CCEaseExponentialIn::reverse(void)

{

    return CCEaseExponentialOut::create(m_pInner->reverse());

}

void CCEaseExponentialOut::update(float time)

{

    m_pInner->update(time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1));

}


CCActionInterval* CCEaseExponentialOut::reverse(void)

{

    return CCEaseExponentialIn::create(m_pInner->reverse());

}

void CCEaseExponentialInOut::update(float time)

{

    time /= 0.5f;

    if (time < 1)

    {

        time = 0.5f * powf(2, 10 * (time - 1));

    }

    else

    {

        time = 0.5f * (-powf(2, -10 * (time - 1)) + 2);

    }


    m_pInner->update(time);

}


CCActionInterval* CCEaseExponentialInOut::reverse()

{

    return CCEaseExponentialInOut::create(m_pInner->reverse());

}


void CCEaseSineIn::update(float time)

{

    m_pInner->update(-1 * cosf(time * (float)M_PI_2) + 1);

}


CCActionInterval* CCEaseSineIn::reverse(void)

{

    return CCEaseSineOut::create(m_pInner->reverse());

}

void CCEaseSineOut::update(float time)

{

    m_pInner->update(sinf(time * (float)M_PI_2));

}


CCActionInterval* CCEaseSineOut::reverse(void)

{

    return CCEaseSineIn::create(m_pInner->reverse());

}

void CCEaseSineInOut::update(float time)

{

    m_pInner->update(-0.5f * (cosf((float)M_PI * time) - 1));

}


CCActionInterval* CCEaseSineInOut::reverse()

{

    return CCEaseSineInOut::create(m_pInner->reverse());

}


CCActionInterval* CCEaseElastic::reverse(void)

{

    CCAssert(0, "Override me");


    return NULL;

}

void CCEaseElasticIn::update(float time)

{

    float newT = 0;

    if (time == 0 || time == 1)

    {

        newT = time;

    }

    else

    {

        float s = m_fPeriod / 4;

        time = time - 1;

        newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod); 

sin 是用的一般的角度计算的,而 sinf 是用弧度作单位计算的

    }


    m_pInner->update(newT);

}


CCActionInterval* CCEaseElasticIn::reverse(void)

{

    return CCEaseElasticOut::create(m_pInner->reverse(), m_fPeriod);

}


void CCEaseElasticOut::update(float time)

{

    float newT = 0;

    if (time == 0 || time == 1)

    {

        newT = time;

    }

    else

    {

        float s = m_fPeriod / 4;

        newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) + 1;

    }


    m_pInner->update(newT);

}


CCActionInterval* CCEaseElasticOut::reverse(void)

{

    return CCEaseElasticIn::create(m_pInner->reverse(), m_fPeriod);

}

void CCEaseElasticInOut::update(float time)

{

    float newT = 0;

    if (time == 0 || time == 1)

    {

        newT = time;

    }

    else

    {

        time = time * 2;

        if (! m_fPeriod)

        {

            m_fPeriod = 0.3f * 1.5f;

        }


        float s = m_fPeriod / 4;


        time = time - 1;

        if (time < 0)

        {

            newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / m_fPeriod);

        }

        else

        {

            newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) * 0.5f + 1;

        }

    }


    m_pInner->update(newT);

}


CCActionInterval* CCEaseElasticInOut::reverse(void)

{

    return CCEaseElasticInOut::create(m_pInner->reverse(), m_fPeriod);

}

float CCEaseBounce::bounceTime(float time)

{

    if (time < 1 / 2.75)

    {

        return 7.5625f * time * time;

    } else 

    if (time < 2 / 2.75)

    {

        time -= 1.5f / 2.75f;

        return 7.5625f * time * time + 0.75f;

    } else

    if(time < 2.5 / 2.75)

    {

        time -= 2.25f / 2.75f;

        return 7.5625f * time * time + 0.9375f;

    }


    time -= 2.625f / 2.75f;

    return 7.5625f * time * time + 0.984375f;

}


CCActionInterval* CCEaseBounce::reverse()

{

    return CCEaseBounce::create(m_pInner->reverse());

}


void CCEaseBounceIn::update(float time)

{

    float newT = 1 - bounceTime(1 - time);

    m_pInner->update(newT);

}


CCActionInterval* CCEaseBounceIn::reverse(void)

{

    return CCEaseBounceOut::create(m_pInner->reverse());

}

void CCEaseBounceOut::update(float time)

{

    float newT = bounceTime(time);

    m_pInner->update(newT);

}


CCActionInterval* CCEaseBounceOut::reverse(void)

{

    return CCEaseBounceIn::create(m_pInner->reverse());

}

void CCEaseBounceInOut::update(float time)

{

    float newT = 0;

    if (time < 0.5f)

    {

        time = time * 2;

        newT = (1 - bounceTime(1 - time)) * 0.5f;

    }

    else

    {

        newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f;

    }


    m_pInner->update(newT);

}


CCActionInterval* CCEaseBounceInOut::reverse()

{

    return CCEaseBounceInOut::create(m_pInner->reverse());

}


void CCEaseBackIn::update(float time)

{

    float overshoot = 1.70158f;

    m_pInner->update(time * time * ((overshoot + 1) * time - overshoot));

}


CCActionInterval* CCEaseBackIn::reverse(void)

{

    return CCEaseBackOut::create(m_pInner->reverse());

}



void CCEaseBackOut::update(float time)

{

    float overshoot = 1.70158f;


    time = time - 1;

    m_pInner->update(time * time * ((overshoot + 1) * time + overshoot) + 1);

}


CCActionInterval* CCEaseBackOut::reverse(void)

{

    return CCEaseBackIn::create(m_pInner->reverse());

}


void CCEaseBackInOut::update(float time)

{

    float overshoot = 1.70158f * 1.525f;


    time = time * 2;

    if (time < 1)

    {

        m_pInner->update((time * time * ((overshoot + 1) * time - overshoot)) / 2);

    }

    else

    {

        time = time - 2;

        m_pInner->update((time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1);

    }

}


CCActionInterval* CCEaseBackInOut::reverse()

{

    return CCEaseBackInOut::create(m_pInner->reverse());

}







这篇关于CCActionEase(可以对动作进行包装 使动作的in和out速度发生改变 使动作更平滑)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/661631

相关文章

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M