本文主要是介绍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速度发生改变 使动作更平滑)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!