写这篇文章,没有花我多少时间。但是由于我的笔记写在云笔记中,图片也多。csdn也不支持直接复制粘贴图片,可以说把笔记移植过来比我当初写还要费事,希望改进吧
从入口开始分析,我们来看看addaction函数,他接受一个动作,并绑定自己,是否暂停取决于节点的running属性。
我们来具体看看ActionManager这个类
class CC_DLLActionManager : public Ref
{
public:
ActionManager(void);
~ActionManager(void);
void addAction(Action *action, Node*target, bool paused);
void removeAllActions();
void removeAllActionsFromTarget(Node*target);
void removeAction(Action *action);
void removeActionByTag(int tag, Node*target);
void removeAllActionsByTag(int tag, Node*target);
Action* getActionByTag(int tag, const Node*target) const;
ssize_tgetNumberOfRunningActionsInTarget(const Node *target) const;
void pauseTarget(Node *target);
void resumeTarget(Node *target);
Vector<Node*>pauseAllRunningActions();
void resumeTargets(constVector<Node*>& targetsToResume);
void update(float dt);
protected:
void removeActionAtIndex(ssize_t index,struct _hashElement *element);
void deleteHashElement(struct _hashElement*element);
void actionAllocWithHashElement(struct_hashElement *element);
protected:
struct _hashElement *_targets;
struct_hashElement *_currentTarget;
bool _currentTargetSalvaged;
};
struct _hashElement *_targets;
struct_hashElement *_currentTarget;
如果是对定时器数据结构很熟悉的人没看到这个结构体应该不会陌生。的确,内部结构是相同的,只不过把之前的定时器对象改为现在的action.
类似的,每个hash节点一般由一个target标志,根据target找到节点。每个节点弱引用一个动作的array,也就是说,每个节点可以绑定多个动作,paused记录了当前动作的暂停状态。
下面来看看addAction函数:
voidActionManager::addAction(Action *action, Node *target, bool paused)
{
CCASSERT(action != nullptr, "");
CCASSERT(target != nullptr, "");
//查找hash表,看看是否有相同的target,如果没有新建一个插入表中。
tHashElement *element = nullptr;
// we should convert it to Ref*, because wesave it as Ref*
Ref *tmp = target;
HASH_FIND_PTR(_targets, &tmp, element);
if (! element)
{
element =(tHashElement*)calloc(sizeof(*element), 1);
element->paused = paused;
target->retain();
element->target = target;
HASH_ADD_PTR(_targets, target,element);
}
//为弱引用的数组分配内存。
actionAllocWithHashElement(element);
//同一个动作结束前,如果被同一个节点包含两次,触发断言
CCASSERT(!ccArrayContainsObject(element->actions, action), "");
//将新的动作加入数组
ccArrayAppendObject(element->actions,action);
action->startWithTarget(target);
}
现在我们不介绍最后一句,因为我们需要回过头去看看Action及其子类
Action:
class CC_DLL Action: public Ref, public Clonable
{
public:
//动作的默认tag,一般不可以用默认tag索引动作
static const int INVALID_TAG = -1;
/**
* @js NA
* @lua NA
*/
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 theexactly 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 willalso set the target.
//将_tagget和originTarget都赋值为新的绑定对象
virtual void startWithTarget(Node *target);
/**
called after the action has finished. Itwill set the ‘target‘ to nil.
IMPORTANT: You should never call"[action stop]" manually. Instead, use:"target->stopAction(action);"
*/
//动作结束后,调用该函数,把_target设置为null
virtual void stop();
//! called every frame with it‘s deltatime. DON‘T override unless you know what you are doing.
//类似定时器中的update函数
virtual void step(float dt);
/**
called once per frame. time a value between0 and 1
For example:
- 0 means that the action just started
- 0.5 means that the action is in themiddle
- 1 means that the action is over
*/
//类似定时器中的trigger函数
virtual void update(float time);
inline Node* getTarget() const { return_target; }
/** The action will modify the targetproperties. */
inline void setTarget(Node *target) {_target = target; }
inline Node* getOriginalTarget() const {return _originalTarget; }
/** Set the original target, since targetcan be nil.
Is the target that were used to run theaction. Unless you are doing something complex, like ActionManager, you shouldNOT 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, targetwill be set to nil.
The target is ‘assigned‘, it is not‘retained‘.
*/
Node *_target;
/** The action tag. An identifier of theaction */
int _tag;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Action);
};
isDown,update,step这三个函数是虚函数,action并没有做什么实际的事情,所以我们现在还不知道他干了些什么,我们之后去看看具体的动作类就会清楚了。
_originalTarget,_target这两者有什么区分现在也不清楚
我们来看看最重要的一个子类FiniteTimeAction:
class CC_DLLFiniteTimeAction : public Action
{
public:
//! get duration in seconds of the action
inline float getDuration() const { return_duration; }
//! set duration in seconds of the action
inline void setDuration(float duration) {_duration = duration; }
//
// Overrides
//
virtual FiniteTimeAction* reverse() constoverride
{
CC_ASSERT(0);
return nullptr;
}
virtual FiniteTimeAction* clone() constoverride
{
CC_ASSERT(0);
return nullptr;
}
CC_CONSTRUCTOR_ACCESS:
FiniteTimeAction()
: _duration(0)
{}
virtual ~FiniteTimeAction(){}
protected:
//! duration in seconds
float _duration;
private:
CC_DISALLOW_COPY_AND_ASSIGN(FiniteTimeAction);
};
只是增加了一个_duration属性,也就是动作的持续时间,应该是为了他的延时动作子类准备的。
我么接着看ActionInstant这个瞬时动作:
class CC_DLLActionInstant : public FiniteTimeAction //<NSCopying>
{
public:
//
// Overrides
//
virtual ActionInstant* clone() constoverride
{
CC_ASSERT(0);
return nullptr;
}
virtual ActionInstant * reverse() constoverride
{
CC_ASSERT(0);
return nullptr;
}
virtual bool isDone() const override;
virtual void step(float dt) override;
virtual void update(float time) override;
};
瞬时动作类重写了step函数和isDown函数,其他的isDone和update函数并没有做出变化
step:
调用了update函数,并传入参数1。我们可以看看update函数的注释
called once perframe. 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
*/
1表示动作结束了
isDown:
isDown也返回true。我们或许会疑惑,不过接下来看了ActionManager中的update函数之后你就会明白了:
该函数在导演类的init函数中被注册到定时器:
该函数有最高的定时触发优先级,每一帧动作的运行都会触发该函数。
voidActionManager::update(float dt)
{
for (tHashElement *elt = _targets; elt !=nullptr; )
{
_currentTarget = elt;
_currentTargetSalvaged = false;
if (! _currentTarget->paused)
{
// The ‘actions‘ MutableArray maychange while inside this loop.
for (_currentTarget->actionIndex= 0; _currentTarget->actionIndex < _currentTarget->actions->num;
_currentTarget->actionIndex++)
{
_currentTarget->currentAction =(Action*)_currentTarget->actions->arr[_currentTarget->actionIndex];
if(_currentTarget->currentAction == nullptr)
{
continue;
}
_currentTarget->currentActionSalvaged = false;
_currentTarget->currentAction->step(dt);
if(_currentTarget->currentActionSalvaged)
{
// The currentAction toldthe node to remove it. To prevent the action from
// accidentallydeallocating itself before finishing its step, we retained
// it. Now that step isdone, it‘s safe to release it.
_currentTarget->currentAction->release();
} else
if (_currentTarget->currentAction->isDone())
{
_currentTarget->currentAction->stop();
Action *action =_currentTarget->currentAction;
// Make currentAction nilto prevent removeAction from salvaging it.
_currentTarget->currentAction = nullptr;
removeAction(action);
}
_currentTarget->currentAction = nullptr;
}
}
// elt, at this moment, is still valid
// so it is safe to ask this here(issue #490)
elt = (tHashElement*)(elt->hh.next);
// only delete currentTarget if noactions were scheduled during the cycle (issue #481)
if (_currentTargetSalvaged &&_currentTarget->actions->num == 0)
{
deleteHashElement(_currentTarget);
}
}
// issue #635
_currentTarget = nullptr;
}
我们可以看到,step就相当于一个触发函数,而动作中的update函数就是触发函数中调用的函数,出发完成后,判断动作是否结束,如果结束调用stop函数,并移除动作。
我们现在可以回到之前的瞬时动作,update函数被传值为1,isDown返回true,就很好理解了。
瞬时动作的基类没有实现update函数,我们以一个具体的瞬时动作来做例子:
class CC_DLL Show : public ActionInstant
{
public:
/** Allocates and initializes the action */
static Show * create();
//
// Overrides
//
virtual void update(float time) override;
virtual ActionInstant* reverse() constoverride;
virtual Show* clone() const override;
CC_CONSTRUCTOR_ACCESS:
Show(){}
virtual ~Show(){}
private:
CC_DISALLOW_COPY_AND_ASSIGN(Show);
};
重写了update函数:
接着看
void ActionManager::update(float dt)中的那个removeAction函数:
好了一目了然,之前的_originTarget和_targrt的区别也就知道了,_target是为了实现节点动作用的,二_target置为空之后,_originTarget便可以用来删除这个动作,动作删除后被release。
好了,我们可以接着看延时动作了:
class CC_DLLActionInterval : public FiniteTimeAction
{
public:
/** how many seconds had elapsed since theactions started to run. */
inline float getElapsed(void) { return_elapsed; }
//extension in GridAction
void setAmplitudeRate(float amp);
float getAmplitudeRate(void);
//
// Overrides
//
virtual bool isDone(void) const override;
virtual voidstep(float dt) override;
virtual void startWithTarget(Node *target)override;
virtual ActionInterval* reverse() constoverride
{
CC_ASSERT(0);
return nullptr;
}
virtual ActionInterval *clone() constoverride
{
CC_ASSERT(0);
return nullptr;
}
CC_CONSTRUCTOR_ACCESS:
/** initializes the action */
bool initWithDuration(float d);
protected:
float_elapsed;
bool _firstTick;
};
_elapsed表示延时动作度过的时间
isDone:
很简单,只要度过的时间大于等于动作的生命周期就表示动作结束。
step:
很简单的定时逻辑,ActionManager中的update函数每一次出发后调用step函数,dt一般是每一帧的时间(大多数是1
/60)。每一次调用后,把dt加到_elapsed就可以做到计时了。update的参数被限制在0-1之间,具体的实现得看一个具体的例子:
这里的initWithDuration就是我们创建动作时候传递的动作生命周期。
update:
很简单吧,每次变化后的属性重新设置给Node就达到了动画的目的。
我们接下来看看移除动作:
void removeAction(Action *action):
移除很简单,找到绑定对象,从对象的动作数组中移除即可。
接下来看看Node中的stopAction函数:
函数原理都是一样的,拿其中一个举例:
间接调用了移除动作的函数。
注意事项:不要用action中的stop函数来停止动作,因为判断动作是否结束的标志是isDown函数,并且判断过程还是在动作执行之后。stop函数会将_target置为null,这样运行动作就会试图去修改_target的属性,导致程序奔溃。还有一点就是动作运行结束后会被删除,如果想多次运行动作,请retain。
总结:
动作最重要的几个函数
isDown
step
stop
update
以及ActionManager::update
还有动作的重要属性:
_target
_origintarget
_elapsed