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

相关文章

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

SpringSecurity6.0 如何通过JWTtoken进行认证授权

《SpringSecurity6.0如何通过JWTtoken进行认证授权》:本文主要介绍SpringSecurity6.0通过JWTtoken进行认证授权的过程,本文给大家介绍的非常详细,感兴趣... 目录项目依赖认证UserDetailService生成JWT token权限控制小结之前写过一个文章,从S

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应