Cocos2d之Action类详解

一、声明

        文章中使用到的cocos2d的源代码的版本是cocos2d-x-3.3rc0。

二、主要内容

【Action类简介】

        在cocos2d中,Action类是所有动作的基类。Action类继承了Ref类和Clonable类。

【Action类的声明源码】

        声明的源码在 CCAction.h 文件中,声明如下:

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

    /** returns a clone of action */
    virtual Action* clone() const
    {
        CC_ASSERT(0);
        return nullptr;
    }

    /** returns a new action that performs the exactly the reverse action */
    virtual Action* reverse() const
    {
        CC_ASSERT(0);
        return nullptr;
    }

    //! return true if the action has finished
    virtual bool isDone() const;

    //! called before the action start. It will also set the 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. DON'T override unless you know what you are doing.
    virtual void step(float 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
    */
    virtual void update(float time);
    
    inline Node* getTarget() const { return _target; }
    /** The action will modify the target properties. */
    inline void setTarget(Node *target) { _target = target; }
    
    inline Node* getOriginalTarget() const { return _originalTarget; }
    /** Set the original target, since target can be nil.
    Is the target that were used to run the action. Unless you are doing something complex, like ActionManager, you should NOT call this method.
    The target is 'assigned', it is not 'retained'.
    @since v0.8.2
    */
    inline void setOriginalTarget(Node *originalTarget) { _originalTarget = originalTarget; }

    inline int getTag() const { return _tag; }
    inline void setTag(int tag) { _tag = tag; }

CC_CONSTRUCTOR_ACCESS:
    Action();
    virtual ~Action();

protected:
    Node    *_originalTarget;
    /** The "target".
    The target will be set with the 'startWithTarget' method.
    When the 'stop' method is called, target will be set to nil.
    The target is 'assigned', it is not 'retained'.
    */
    Node    *_target;
    /** The action tag. An identifier of the action */
    int     _tag;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(Action);
};

    Action对象有两个Node对象成员,一个是 _originalTarget 是经常用来执行动作的节点,用于一些复杂的动作,很少用;另一个是 _target ,这是常规的执行动作的节点, 与_originalTarget 不同的是 _target 在动作执行完之后会被置nil。

    值得注意的是,在setTarget 和 setOriginalTarget 函数中,节点指针只是简单的赋值给 _originalTarget 和 _target 而已,并没有调用节点的retain函数使节点对象的引用计数加一。

【Action类的定义源码】

Action::Action()
:_originalTarget(nullptr)
,_target(nullptr)
,_tag(Action::INVALID_TAG)
{
}

Action::~Action()
{
    CCLOGINFO("deallocing Action: %p - tag: %i", this, _tag);
}

std::string Action::description() const
{
    return StringUtils::format("<Action | Tag = %d", _tag);
}

void Action::startWithTarget(Node *aTarget)
{
    _originalTarget = _target = aTarget;
}

void Action::stop()
{
    _target = nullptr;
}

bool Action::isDone() const
{
    return true;
}

void Action::step(float dt)
{
    CC_UNUSED_PARAM(dt);
    CCLOG("[Action step]. override me");
}

void Action::update(float time)
{
    CC_UNUSED_PARAM(time);
    CCLOG("[Action update]. override me");
}

        从定义中可以看出,Action类其实实现很少东西,只是简单定义了动作的一些常规行为而已。

原文地址:https://www.cnblogs.com/chenshi/p/4109520.html