首页 > 代码库 > Cocos2d之Action类详解
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类其实实现很少东西,只是简单定义了动作的一些常规行为而已。
Cocos2d之Action类详解
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。