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

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate