本文来自http://blog.youkuaiyun.com/runaying ,引用必须注明出处!
cocos2d-x节点(CCActionInterval.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
//有时间间隔的 action,使用动画、闪烁、重复次数、按次序创建 action,贝塞尔曲线移动
///cocos2d-x-3.0alpha0/cocos2dx/actions
//有时间间隔的 action,使用动画、闪烁、重复次数、按次序 创建 action,贝塞尔曲线移动
#ifndef __ACTION_CCINTERVAL_ACTION_H__
#define __ACTION_CCINTERVAL_ACTION_H__
#include "base_nodes/CCNode.h"
#include "CCAction.h"
#include "CCProtocols.h"
#include "sprite_nodes/CCSpriteFrame.h"
#include "sprite_nodes/CCAnimation.h"
#include <vector>
NS_CC_BEGIN
/**
* @addtogroup actions
* @{
*/
/**
@brief 一个间隔 action ,一个在确定周期内发生的 action
它有一个开始时间和一个结束时间. 结束时间参数,开始时间加上持续时间
这些 ActionInterval actions 有一些有趣的属性, 如:
- 他们可以正常运行(默认)
- 它们可以使用相反的方法逆转运行
- 它们可以随着运行时间的改变加速, AccelDeccel(加速减速) and Speed(快速) actions.
例如, 你可以模拟一个正常运行的乒乓球效果,然后再使用反向模式运行他
例如:
Action *pingPongAction = Sequence::actions(action, action->reverse(), NULL);
*/
class CC_DLL ActionInterval : public FiniteTimeAction
{
public:
/** 从运行开始经过了多少秒. */
inline float getElapsed(void) { return _elapsed; }
/** 初始化一个 action */
bool initWithDuration(float d);
//在 GridAction 里面扩展
void setAmplitudeRate(float amp);
float getAmplitudeRate(void);
//
// Overrides
//
virtual bool isDone(void) const override;
virtual void step(float dt) override;
virtual void startWithTarget(Node *target) override;
virtual ActionInterval* reverse() const override = 0;
virtual ActionInterval *clone() const override = 0;
protected:
float _elapsed;
bool _firstTick;
};
/** @brief 按照次序运行 actions, 一个接一个的
*/
class CC_DLL Sequence : public ActionInterval
{
public:
/** 辅助构造函数,用来创建一个按照次序运行的 action array(序列) */
static Sequence* create(FiniteTimeAction *pAction1, ...) CC_REQUIRES_NULL_TERMINATION;
/** 辅助构造函数,使用给丁的 array 创建一个按照次序运行的 action array(序列)
* @code
* 当这个函数绑定到 js or lua,输入参数会改变
* in js :var create(var object1,var object2, ...)
* in lua :local create(local object1,local object2, ...)
* @endcode
*/
static Sequence* create(Array *arrayOfActions);
/** 辅助构造函数,用来创建一个 sequence-able actions array */
static Sequence* createWithVariableList(FiniteTimeAction *pAction1, va_list args);
/** creates the action */
static Sequence* createWithTwoActions(FiniteTimeAction *pActionOne, FiniteTimeAction *pActionTwo);
/**
* @js NA
* @lua NA
*/
virtual ~Sequence(void);
/** initializes the action */
bool initWithTwoActions(FiniteTimeAction *pActionOne, FiniteTimeAction *pActionTwo);
//
// Overrides
//
virtual Sequence* clone() const override;
virtual Sequence* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float t) override;
protected:
FiniteTimeAction *_actions[2];
float _split;
int _last;
};
/** @brief action 重复的次数.
* 如果一直使用下去使用 RepeatForever action.
*/
class CC_DLL Repeat : public ActionInterval
{
public:
/** 创建一个重复 action. Times 是一个 unsigned(无符号)整数,在 1 、 pow(2,30) 之间 */
static Repeat* create(FiniteTimeAction *pAction, unsigned int times);
/**
* @js NA
* @lua NA
*/
virtual ~Repeat(void);
/** 创建一个重复 action. Times 是一个 unsigned(无符号)整数,在 1 、 pow(2,30) 之间 */
bool initWithAction(FiniteTimeAction *pAction, unsigned int times);
inline void setInnerAction(FiniteTimeAction *pAction)
{
if (_innerAction != pAction)
{
CC_SAFE_RETAIN(pAction);
CC_SAFE_RELEASE(_innerAction);
_innerAction = pAction;
}
}
inline FiniteTimeAction* getInnerAction()
{
return _innerAction;
}
//
// Overrides
//
virtual Repeat* clone() const override;
virtual Repeat* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float dt) override;
virtual bool isDone(void) const override;
protected:
unsigned int _times;
unsigned int _total;
float _nextDt;
bool _actionInstant;
/** Inner action */
FiniteTimeAction *_innerAction;
};
/** @brief 永远重复一个动作
在限定次数重复同一个 action
@warning 这个动作不能是 Sequenceable 因为它不是一个 IntervalAction
*/
class CC_DLL RepeatForever : public ActionInterval
{
public:
/** creates the action */
static RepeatForever* create(ActionInterval *pAction);
/**
* @js ctor
*/
RepeatForever()
: _innerAction(NULL)
{}
/**
* @js NA
* @lua NA
*/
virtual ~RepeatForever();
/** initializes the action */
bool initWithAction(ActionInterval *pAction);
inline void setInnerAction(ActionInterval *pAction)
{
if (_innerAction != pAction)
{
CC_SAFE_RELEASE(_innerAction);
_innerAction = pAction;
CC_SAFE_RETAIN(_innerAction);
}
}
inline ActionInterval* getInnerAction()
{
return _innerAction;
}
//
// Overrides
//
virtual RepeatForever* clone() const override;
virtual RepeatForever* reverse(void) const override;
virtual void startWithTarget(Node* target) override;
virtual void step(float dt) override;
virtual bool isDone(void) const override;
protected:
/** Inner(内部) action */
ActionInterval *_innerAction;
};
/** @brief 立即产生一个新的 action
*/
class CC_DLL Spawn : public ActionInterval
{
public:
/** 辅助构造函数,用来创建一个 spawned actions 数组
* @code
* 当这个函数绑定到 js or lua,输入参数会改变
* in js :var create(var object1,var object2, ...)
* in lua :local create(local object1,local object2, ...)
* @endcode
*/
static Spawn* create(FiniteTimeAction *pAction1, ...) CC_REQUIRES_NULL_TERMINATION;
/** 辅助构造函数,用来创建一个 spawned actions */
static Spawn* createWithVariableList(FiniteTimeAction *pAction1, va_list args);
/** 辅助构造函数,使用给定的 array 创建一个 spawned actions 数组 */
static Spawn* create(Array *arrayOfActions);
/** creates the Spawn action */
static Spawn* createWithTwoActions(FiniteTimeAction *pAction1, FiniteTimeAction *pAction2);
/**
* @js NA
* @lua NA
*/
virtual ~Spawn(void);
/** 使用 两个 FiniteTimeAction 初始化 Spawn action */
bool initWithTwoActions(FiniteTimeAction *pAction1, FiniteTimeAction *pAction2);
//
// Overrides
//
virtual Spawn* clone() const override;
virtual Spawn* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float time) override;
protected:
FiniteTimeAction *_one;
FiniteTimeAction *_two;
};
/** @brief 通过修改旋转属性,把一个 Node 对象旋转特定的角度
方向将决定 shortest(最短)angle.
*/
class CC_DLL RotateTo : public ActionInterval
{
public:
/** 使用 单独的旋转角度 创建一个 action */
static RotateTo* create(float fDuration, float fDeltaAngleX, float fDeltaAngleY);
/** creates the action */
static RotateTo* create(float fDuration, float fDeltaAngle);
/** initializes the action */
bool initWithDuration(float fDuration, float fDeltaAngle);
bool initWithDuration(float fDuration, float fDeltaAngleX, float fDeltaAngleY);
//
// Overrides
//
virtual RotateTo* clone() const override;
virtual RotateTo* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
float _dstAngleX;
float _startAngleX;
float _diffAngleX;
float _dstAngleY;
float _startAngleY;
float _diffAngleY;
};
/** @brief 顺时针旋转一个对象到特定的角度,特过这个你可以修改他的旋转属性
*/
class CC_DLL RotateBy : public ActionInterval
{
public:
/** creates the action */
static RotateBy* create(float fDuration, float fDeltaAngle);
/** initializes the action */
bool initWithDuration(float fDuration, float fDeltaAngle);
static RotateBy* create(float fDuration, float fDeltaAngleX, float fDeltaAngleY);
bool initWithDuration(float fDuration, float fDeltaAngleX, float fDeltaAngleY);
//
// Override
//
virtual RotateBy* clone() const override;
virtual RotateBy* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
float _angleX;
float _startAngleX;
float _angleY;
float _startAngleY;
};
/** 通过修改节点的位置,移动它们的 x,y 像素
x 、 y 相对于对象的位置.
可以同时调用几个 MoveBy action ,最后所得的运动将是所有单个运动的综合
@since v2.1beta2-custom
*/
class CC_DLL MoveBy : public ActionInterval
{
public:
/** creates the action */
static MoveBy* create(float duration, const Point& deltaPosition);
/** initializes the action */
bool initWithDuration(float duration, const Point& deltaPosition);
//
// Overrides
//
virtual MoveBy* clone() const override;
virtual MoveBy* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
Point _positionDelta;
Point _startPosition;
Point _previousPosition;
};
/** 移动节点对象的 x,y 位置,x,y 通过修改他的位置属性的绝对坐标。
可以同时调用几个 MoveBy action ,最后所得的运动将是所有单个运动的综合
@since v2.1beta2-custom
*/
class CC_DLL MoveTo : public MoveBy
{
public:
/** creates the action */
static MoveTo* create(float duration, const Point& position);
/** initializes the action */
bool initWithDuration(float duration, const Point& position);
//
// Overrides
//
virtual MoveTo* clone() const override;
virtual void startWithTarget(Node *target) override;
protected:
Point _endPosition;
};
/** 通过修改他的 skewX(倾斜)、skewY(倾斜)属性,倾斜节点对象到给定的角度
@since v1.0
*/
class CC_DLL SkewTo : public ActionInterval
{
public:
/** creates the action */
static SkewTo* create(float t, float sx, float sy);
SkewTo();
bool initWithDuration(float t, float sx, float sy);
//
// Overrides
//
virtual SkewTo* clone() const override;
virtual SkewTo* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
float _skewX;
float _skewY;
float _startSkewX;
float _startSkewY;
float _endSkewX;
float _endSkewY;
float _deltaX;
float _deltaY;
};
/** 通过 skewX(倾斜)、skewY(倾斜)角度 Skews(倾斜)Node 对象
@since v1.0
*/
class CC_DLL SkewBy : public SkewTo
{
public:
/** creates the action */
static SkewBy* create(float t, float deltaSkewX, float deltaSkewY);
bool initWithDuration(float t, float sx, float sy);
//
// Overrides
//
virtual void startWithTarget(Node *target) override;
virtual SkewBy* clone() const override;
virtual SkewBy* reverse(void) const override;
};
/** @brief 通过修改他的位置属性,移动节点对象模拟抛跳运动
*/
class CC_DLL JumpBy : public ActionInterval
{
public:
/** creates the action */
static JumpBy* create(float duration, const Point& position, float height, int jumps);
/** initializes the action */
bool initWithDuration(float duration, const Point& position, float height, int jumps);
//
// Overrides
//
virtual JumpBy* clone() const override;
virtual JumpBy* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
Point _startPosition;
Point _delta;
float _height;
int _jumps;
Point _previousPos;
};
/** @brief 通过修改位他的置属性,移动Node对象到一个抛物线的位置模拟跳跃运动 */
class CC_DLL JumpTo : public JumpBy
{
public:
/** creates the action */
static JumpTo* create(float duration, const Point& position, float height, int jumps);
//
// Override
//
virtual void startWithTarget(Node *target) override;
virtual JumpTo* clone() const override;
virtual JumpTo* reverse(void) const override;
};
/** Bezier(贝塞尔)配置结构
*/
typedef struct _ccBezierConfig {
//! end position of the bezier
Point endPosition;
//! Bezier control point 1
Point controlPoint_1;
//! Bezier control point 2
Point controlPoint_2;
} ccBezierConfig;
/** @brief 使用 cubic Bezier curve(三次方贝塞尔曲线)把目标移动特定距离
*/
class CC_DLL BezierBy : public ActionInterval
{
public:
/** 使用 duration 、Bezier(贝塞尔)配置 创建一个 action
* @code
* 当这个函数绑定到 js or lua,输入参数会改变
* in js: var create(var t,var table)
* in lua: lcaol create(local t, local table)
* @endcode
*/
static BezierBy* create(float t, const ccBezierConfig& c);
/** 使用 duration 、Bezier(贝塞尔)配置 初始化一个 action */
bool initWithDuration(float t, const ccBezierConfig& c);
//
// Overrides
//
virtual BezierBy* clone() const override;
virtual BezierBy* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
ccBezierConfig _config;
Point _startPosition;
Point _previousPosition;
};
/** @brief 使用 cubic Bezier curve(三次方贝塞尔曲线)把目标移动到目标点
@since v0.8.2
*/
class CC_DLL BezierTo : public BezierBy
{
public:
/** 使用 duration 、Bezier(贝塞尔)配置 创建一个 action
* @code
* 当这个函数绑定到 js or lua,输入参数会改变
* in js: var create(var t,var table)
* in lua: lcaol create(local t, local table)
* @endcode
*/
static BezierTo* create(float t, const ccBezierConfig& c);
bool initWithDuration(float t, const ccBezierConfig &c);
//
// Overrides
//
virtual void startWithTarget(Node *target) override;
virtual BezierTo* clone() const override;
virtual BezierTo* reverse(void) const override;
protected:
ccBezierConfig _toConfig;
};
/** @brief 通过修改节点的 scale 属性,修改它的缩放系数
@warning 这个 action 不支持 "reverse"
*/
class CC_DLL ScaleTo : public ActionInterval
{
public:
/** 使用 X,y 上相同的比例因子 创建一个 action */
static ScaleTo* create(float duration, float s);
/** 使用 X,y 上的比例因子 创建一个 action */
static ScaleTo* create(float duration, float sx, float sy);
/** 使用 X,y 上相同的比例因子 初始化一个 action */
bool initWithDuration(float duration, float s);
/** 使用 X,y 上的比例因子 初始化一个 action */
bool initWithDuration(float duration, float sx, float sy);
//
// Overrides
//
virtual ScaleTo* clone() const override;
virtual ScaleTo* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
float _scaleX;
float _scaleY;
float _startScaleX;
float _startScaleY;
float _endScaleX;
float _endScaleY;
float _deltaX;
float _deltaY;
};
/** @brief 通过修改节点的 scale 属性,修改它的缩放系数.
*/
class CC_DLL ScaleBy : public ScaleTo
{
public:
/** 使用 X,y 上相同的比例因子 创建一个 action */
static ScaleBy* create(float duration, float s);
/** 使用 X,y 上的比例因子 创建一个 action */
static ScaleBy* create(float duration, float sx, float sy);
//
// Overrides
//
virtual void startWithTarget(Node *target) override;
virtual ScaleBy* clone() const override;
virtual ScaleBy* reverse(void) const override;
};
/** @brief 通过修改他的 visible(可见)属性,使一个节点对象 Blinks(闪烁)
*/
class CC_DLL Blink : public ActionInterval
{
public:
/** creates the action */
static Blink* create(float duration, int blinks);
/** initializes the action */
bool initWithDuration(float duration, int blinks);
//
// Overrides
//
virtual Blink* clone() const override;
virtual Blink* reverse(void) const override;
virtual void update(float time) override;
virtual void startWithTarget(Node *target) override;
virtual void stop() override;
protected:
int _times;
bool _originalState;
};
/** @brief 这个对象的 Fades 实现了 RGBAProtocol 协议. 它把对象的不透明度从 0 变化到 255
这个 action 和 FadeOut 相反
*/
class CC_DLL FadeIn : public ActionInterval
{
public:
/** creates the action */
static FadeIn* create(float d);
//
// Overrides
//
virtual void update(float time) override;
virtual FadeIn* clone() const override;
virtual ActionInterval* reverse(void) const override;
};
/** @brief 这个对象的 Fades 实现了 RGBAProtocol 协议. 它把对象的不透明度从 255 变化到 0
这个 action 和 FadeIn 相反
*/
class CC_DLL FadeOut : public ActionInterval
{
public:
/** creates the action */
static FadeOut* create(float d);
//
// Overrides
//
virtual void update(float time) override;
virtual FadeOut* clone() const override;
virtual ActionInterval* reverse(void) const override;
};
/** @brief 这个对象的 Fades 实现了 RGBAProtocol 协议. 它把对象的不透明度从当前值,变化到一个自定义的值.
@warning 这个 action 不支持 "reverse"(反向)
*/
class CC_DLL FadeTo : public ActionInterval
{
public:
/** 使用 duration 、 opacity(不透明度)创建一个 action */
static FadeTo* create(float duration, GLubyte opacity);
/** 使用 duration 、 opacity(不透明度)初始化一个 action */
bool initWithDuration(float duration, GLubyte opacity);
//
// Overrides
//
virtual FadeTo* clone() const override;
virtual FadeTo* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
GLubyte _toOpacity;
GLubyte _fromOpacity;
};
/** @brief Tints 实现了 NodeRGB 协议 当前节点是一个自定义的 tint 节点
@warning 这个 action 不支持 "reverse"(反向)
@since v0.7.2
*/
class CC_DLL TintTo : public ActionInterval
{
public:
/** 使用 duration 、 color 创建一个 action */
static TintTo* create(float duration, GLubyte red, GLubyte green, GLubyte blue);
/** 使用 duration 、 color 初始化一个 action */
bool initWithDuration(float duration, GLubyte red, GLubyte green, GLubyte blue);
//
// Overrides
//
virtual TintTo* clone() const override;
virtual TintTo* reverse(void) const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
Color3B _to;
Color3B _from;
};
/** @brief Tints 实现了 NodeRGB 协议 当前节点是一个自定义的 tint 节点
@since v0.7.2
*/
class CC_DLL TintBy : public ActionInterval
{
public:
/** 使用 duration 、 color 创建一个 action */
static TintBy* create(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue);
/** 使用 duration 、 color 初始化一个 action */
bool initWithDuration(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue);
//
// Overrides
//
virtual TintBy* clone() const override;
virtual TintBy* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
protected:
GLshort _deltaR;
GLshort _deltaG;
GLshort _deltaB;
GLshort _fromR;
GLshort _fromG;
GLshort _fromB;
};
/** @brief 延迟 一个确定的秒数 执行的 action
*/
class CC_DLL DelayTime : public ActionInterval
{
public:
/** creates the action */
static DelayTime* create(float d);
//
// Overrides
//
virtual void update(float time) override;
virtual DelayTime* reverse() const override;
virtual DelayTime* clone() const override;
};
/** @brief 以相反的顺序执行 action, 时间从 time=duration 变化到 time=0
@warning Use this action carefully. This action is not
sequenceable. Use it as the default "reversed" method
of your own actions, but using it outside the "reversed"
scope is not recommended.
*/
class CC_DLL ReverseTime : public ActionInterval
{
public:
/** creates the action */
static ReverseTime* create(FiniteTimeAction *pAction);
/**
* @js NA
* @lua NA
*/
virtual ~ReverseTime(void);
/**
* @js ctor
*/
ReverseTime();
/** initializes the action */
bool initWithAction(FiniteTimeAction *pAction);
//
// Overrides
//
virtual ReverseTime* reverse() const override;
virtual ReverseTime* clone() const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float time) override;
protected:
FiniteTimeAction *_other;
};
class Texture2D;
/** @brief 给定动画名的动画精灵 */
class CC_DLL Animate : public ActionInterval
{
public:
/** 使用 动画 创建一个 action ;动画结束时,将恢复原始的帧 */
static Animate* create(Animation *pAnimation);
/**
* @js ctor
*/
Animate();
/**
* @js NA
* @lua NA
*/
virtual ~Animate();
/** 使用 动画 初始化一个 action ;动画结束时,将恢复原始的帧 */
bool initWithAnimation(Animation *pAnimation);
/** sets 动画对象的动画 */
void setAnimation( Animation* animation );
/** returns 动画对象的动画 */
Animation* getAnimation() { return _animation; }
const Animation* getAnimation() const { return _animation; }
//
// Overrides
//
virtual Animate* clone() const override;
virtual Animate* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float t) override;
protected:
std::vector<float>* _splitTimes;
int _nextFrame;
SpriteFrame* _origFrame;
unsigned int _executedLoops;
Animation* _animation;
};
/** 覆盖目标 action,所以它总是在目标上运行
* 指定的 action 被创建 而不是 runAction 制定的.
*/
class CC_DLL TargetedAction : public ActionInterval
{
public:
/**
* @js ctor
*/
TargetedAction();
/**
* @js NA
* @lua NA
*/
virtual ~TargetedAction();
/** 使用 指定 action、forced(被迫)目标 创建一个 action */
static TargetedAction* create(Node* target, FiniteTimeAction* pAction);
/** 使用 指定 action、forced(被迫)目标 初始化一个 action */
bool initWithTarget(Node* target, FiniteTimeAction* pAction);
/** Sets target; action 会强制在目标上运行 */
void setForcedTarget(Node* forcedTarget);
/** returns target; action 会强制在目标上运行 */
Node* getForcedTarget() { return _forcedTarget; }
const Node* getForcedTarget() const { return _forcedTarget; }
//
// Overrides
//
virtual TargetedAction* clone() const override;
virtual TargetedAction* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void stop(void) override;
virtual void update(float time) override;
private:
FiniteTimeAction* _action;
Node* _forcedTarget;
};
// end of actions group
/// @}
NS_CC_END
#endif //__ACTION_CCINTERVAL_ACTION_H__