cocos2d-x Action实现原理

本文深入探讨cocos2d-x中的Action机制,包括Action的分类如FiniteTimeAction、Speed和Follow,以及ActionInterval与ActionInstant的区别。通过实例分析了移动、缩放等动作的实现,并讲解了如何自定义动作,如改变执行速度和实现旋转效果,以保持运动方向与自身方向一致。

Action是一个非常低阶的动画系统,它通过在一段时间内对Node元素的某些属性进行插值计算。
Action的用法就不讲了,现在我想对Action的整个实现以及执行流程分析。
这里写图片描述

动作类的基类是Action,通过继承它可以实现很多种动作。

  FiniteTimeAction:有限次动作执行类,就是按时间顺序执行一系列动作,执行完后动作结束;

  Speed:调整实体(节点)的执行速度;

  Follow:可以使节点跟随指定的另一个节点移动。

FiniteTimeAction又分为ActionInstanse(瞬时动作的基类)和ActionInterval(延时动作的基类)。ActionInstant:没什么特别,跟ActionInterval主要区别是没有执行过程,动作瞬间就执行完成了;ActionInterval:执行需要一定的时间(或者说一个过程)。

根据上面的类结构图,ActionInterval的子类有很多,可以通过cocos2d-x自带的tests例子来学习,主要有这些动作:移动(CCMoveTo/CCMoveBy)、缩放(ScaleTo/ScaleBy)、旋转(RotateTO/RotateBy)、扭曲(SkewTo/SkewBy)、跳跃(JumpTo/CCJumpBy)、贝塞尔曲线(BezierTo/BezierBy)、闪烁(Bink)、淡入淡出(FadeIn/FadeOut)、染色(TintTo/TintBy)等,还可以把上面这些动作的几个组合成一个序列。下面是移动和缩放动作的代码示例,其他的动作都类似,都是调用actionWithDuration函数,用到的时候具体参数的含义可以参考cocos2d-x自带的tests例子。
Action分析:
CCAction.h

class CC_DLL Action : public Ref, public Clonable
{
public:
    /** Default tag used for all the actions. */
    static const int INVALID_TAG = -1;
    /**
     * @js NA
     * @lua NA
     */
    virtual std::string description() const;

    /** Returns a clone of action.
     *
     * @return A clone action.
     */
    virtual Action* clone() const
    {
        CC_ASSERT(0);
        return nullptr;
    }

    /** Returns a new action that performs the exactly the reverse action. 
     *
     * @return A new action that performs the exactly the reverse action.
     * @js NA
     */
    virtual Action* reverse() const //逆动作
    {
        CC_ASSERT(0);
        return nullptr;
    }

    /** Return true if the action has finished. 
     * 
     * @return Is true if the action has finished.
     */
    virtual bool isDone() const;

    /** Called before the action start. It will also set the target. 
     *
     * @param target A certain target.
     */
    virtual void startWithTarget(Node *target);//设置动作执行节点

    /** 
     * Called after the action has finished. It will set the 'target' to nil.
     * IMPORTANT: You should never call "Action::stop()" manually. Instead, use: "target->stopAction(action);".
     */
    virtual void stop();

    /** Called every frame with it's delta time, dt in seconds. DON'T override unless you know what you are doing. 
     *
     * @param dt In seconds.
     */
    virtual void step(float dt); //每帧被调用 dt时间间隔

    /** 
     * Called once per frame. time a value between 0 and 1.

     * For example:
     * - 0 Means that the action just started.
     * - 0.5 Means that the action is in the middle.
     * - 1 Means that the action is over.
     *
     * @param time A value between 0 and 1.
     */
    virtual void update(float time);//step调用传入动作进度
    /** Return certain target.
     *
     * @return A certain target.
     */
    inline Node* getTarget() const { return _target; }
    /** The action will modify the target properties. 
     *
     * @param target A certain target.
     */
    inline void setTarget(Node *target) { _target = target; }
    /** Return a original Target. 
     *
     * @return A original Target.
     */
    inline Node* getOriginalTarget() const { 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值