写这篇文章,没有花我多少时间。但是由于我的笔记写在云笔记中,图片也多。csdn也不支持直接复制粘贴图片,可以说把笔记移植过来比我当初写还要费事,希望改进吧
从入口开始分析,我们来看看@H_403_15@addaction@H_403_15@函数,他接受一个动作,并绑定自己,是否暂停取决于节点的@H_403_15@running@H_403_15@属性。@H_403_15@
我们来具体看看@H_403_15@ActionManager@H_403_15@这个类@H_403_15@
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;@H_403_15@
struct_hashElement *_currentTarget;@H_403_15@
bool _currentTargetSalvaged;
};
struct _hashElement*_targets;@H_403_15@
struct_hashElement *_currentTarget;@H_403_15@
如果是对定时器数据结构很熟悉的人没看到这个结构体应该不会陌生。的确,内部结构是相同的,只不过把之前的定时器对象改为现在的@H_403_15@action.@H_403_15@
类似的,每个@H_403_15@hash@H_403_15@节点一般由一个@H_403_15@target@H_403_15@标志,根据@H_403_15@target@H_403_15@找到节点。每个节点弱引用一个动作的@H_403_15@array@H_403_15@,也就是说,每个节点可以绑定多个动作,@H_403_15@paused@H_403_15@记录了当前动作的暂停状态。@H_403_15@
下面来看看@H_403_15@addAction@H_403_15@函数:@H_403_15@
voidActionManager::addAction(Action *action,Node *target,bool paused)
{
CCASSERT(action != nullptr,"");
CCASSERT(target != nullptr,"");
//@H_403_15@查找@H_403_15@hash@H_403_15@表,看看是否有相同的@H_403_15@target@H_403_15@,如果没有新建一个插入表中。@H_403_15@
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);
}
//@H_403_15@为弱引用的数组分配内存@H_403_15@。@H_403_15@
actionAllocWithHashElement(element);@H_403_15@
//@H_403_15@同一个动作结束前,如果被同一个节点包含两次,触发断言@H_403_15@
CCASSERT(!ccArrayContainsObject(element->actions,action),"");@H_403_15@
//@H_403_15@将新的动作加入数组@H_403_15@
ccArrayAppendObject(element->actions,action);@H_403_15@
action->startWithTarget(target);
}
现在我们不介绍最后一句,因为我们需要回过头去看看@H_403_15@Action@H_403_15@及其子类@H_403_15@
class CC_DLL Action: public Ref,public Clonable
{
public:
@H_403_15@//@H_403_15@动作的默认@H_403_15@tag@H_403_15@,一般不可以用默认@H_403_15@tag@H_403_15@索引动作@H_403_15@
static const int INVALID_TAG = -1;@H_403_15@
/**
* @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
//@H_403_15@判断动作是否结束@H_403_15@
virtual bool isDone() const;@H_403_15@
//! called before the action start. It willalso set the target.
//@H_403_15@将@H_403_15@_tagget@H_403_15@和@H_403_15@originTarget@H_403_15@都赋值为新的绑定对象@H_403_15@
virtual void startWithTarget(Node *target);@H_403_15@
/**
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);"
*/
//@H_403_15@动作结束后,调用该函数,把@H_403_15@_target@H_403_15@设置为@H_403_15@null@H_403_15@
virtual void stop();@H_403_15@
//! called every frame with it's deltatime. DON'T override unless you know what you are doing.
//@H_403_15@类似定时器中的@H_403_15@update@H_403_15@函数@H_403_15@
virtual void step(float dt);@H_403_15@
/**
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
*/
//@H_403_15@类似定时器中的@H_403_15@trigger@H_403_15@函数@H_403_15@
virtual void update(float time);@H_403_15@
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;@H_403_15@
/** 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;@H_403_15@
/** The action tag. An identifier of theaction */
int_tag;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Action);
};
isDown@H_403_15@,@H_403_15@update@H_403_15@,@H_403_15@step@H_403_15@这三个函数是虚函数,@H_403_15@action@H_403_15@并没有做什么实际的事情,所以我们现在还不知道他干了些什么,我们之后去看看具体的动作类就会清楚了。@H_403_15@
_originalTarget@H_403_15@@H_403_15@,@H_403_15@_target@H_403_15@@H_403_15@这两者有什么区分现在也不清楚@H_403_15@
我们来看看最重要的一个子类@H_403_15@FiniteTimeAction@H_403_15@:@H_403_15@
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;@H_403_15@
private:
CC_DISALLOW_COPY_AND_ASSIGN(FiniteTimeAction);
};
只是增加了一个@H_403_15@_duration@H_403_15@属性,也就是动作的持续时间,应该是为了他的延时动作子类准备的。@H_403_15@
我么接着看@H_403_15@ActionInstant@H_403_15@这个瞬时动作:@H_403_15@
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;@H_403_15@
virtual void step(float dt) override;@H_403_15@
virtual void update(float time) override;
};
瞬时动作类@H_403_15@@H_403_15@重写了@H_403_15@step@H_403_15@函数和@H_403_15@isDown@H_403_15@函数,其他的@H_403_15@isDone@H_403_15@和@H_403_15@update@H_403_15@函数并没有做出变化@H_403_15@
step:
调用了@H_403_15@update@H_403_15@函数,并传入参数@H_403_15@1@H_403_15@。我们可以看看@H_403_15@update@H_403_15@函数的注释@H_403_15@
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
*/
isDown:
isDown@H_403_15@也返回@H_403_15@true@H_403_15@。我们或许会疑惑,不过接下来看了@H_403_15@ActionManager@H_403_15@中的@H_403_15@update@H_403_15@函数之后你就会明白了:@H_403_15@
该函数在导演类的@H_403_15@init@H_403_15@函数中被注册到定时器:@H_403_15@
该函数有最高的定时触发优先级,每一帧动作的运行都会触发该函数。@H_403_15@
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);@H_403_15@
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()@H_403_15@)
{
_currentTarget->currentAction->stop();@H_403_15@
Action *action =_currentTarget->currentAction;
// Make currentAction nilto prevent removeAction from salvaging it.
_currentTarget->currentAction = nullptr;
removeAction(action);@H_403_15@
}
_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;
}
我们可以看到,@H_403_15@step@H_403_15@就相当于一个触发函数,而动作中的@H_403_15@update@H_403_15@函数就是触发函数中调用的函数,出发完成后,判断动作是否结束,如果结束调用@H_403_15@stop@H_403_15@函数,并移除动作。@H_403_15@
我们现在可以回到之前的瞬时动作,@H_403_15@update@H_403_15@函数被传值为@H_403_15@1@H_403_15@,@H_403_15@isDown@H_403_15@返回@H_403_15@true@H_403_15@,就很好理解了。@H_403_15@
瞬时动作的基类没有实现@H_403_15@update@H_403_15@函数,我们以一个具体的瞬时动作来做例子:@H_403_15@
c@H_403_15@lass CC_DLL Show : public ActionInstant@H_403_15@
{
public:
/** Allocates and initializes the action */
static Show * create();
//
// Overrides
//
virtual void update(float time) override;@H_403_15@
virtual ActionInstant* reverse() constoverride;
virtual Show* clone() const override;
CC_CONSTRUCTOR_ACCESS:
Show(){}
virtual ~Show(){}
private:
CC_DISALLOW_COPY_AND_ASSIGN(Show);
};
重写了@H_403_15@update@H_403_15@函数:@H_403_15@
接着看
void ActionManager::update(float dt)@H_403_15@中的那个@H_403_15@removeAction@H_403_15@函数:@H_403_15@
@H_403_15@
好了一目了然,之前的@H_403_15@_originTarget@H_403_15@和@H_403_15@_targrt@H_403_15@的区别也就知道了,@H_403_15@_target@H_403_15@是为了实现节点动作用的,二@H_403_15@_target@H_403_15@置为空之后,@H_403_15@_originTarget@H_403_15@便可以用来删除这个动作,动作删除后被@H_403_15@release@H_403_15@。@H_403_15@
好了,我们可以接着看延时动作了:
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;@H_403_15@
virtual voidstep(float dt) override;@H_403_15@
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;@H_403_15@
bool_firstTick;
};
_elapsed表示延时动作度过的时间
isDone:
@H_403_15@
很简单,只要度过的时间大于等于动作的生命周期就表示动作结束。
step:
很简单的定时逻辑,@H_403_15@ActionManager@H_403_15@中的@H_403_15@update@H_403_15@函数每一次出发后调用@H_403_15@step@H_403_15@函数,@H_403_15@dt@H_403_15@一般是每一帧的时间(大多数是@H_403_15@1 /60@H_403_15@)。每一次调用后,把@H_403_15@dt@H_403_15@加到@H_403_15@_elapsed@H_403_15@就可以做到计时了。@H_403_15@update@H_403_15@的参数被限制在@H_403_15@0-1@H_403_15@之间@H_403_15@,具体的实现得看一个具体的例子:@H_403_15@
这里的@H_403_15@initWithDuration@H_403_15@就是我们创建动作时候传递的动作生命周期。@H_403_15@
很简单吧,每次变化后的属性重新设置给@H_403_15@Node@H_403_15@就达到了动画的目的。@H_403_15@
我们接下来看看移除动作:@H_403_15@
void removeAction(Action *action)@H_403_15@:@H_403_15@
移除很简单,找到绑定对象,从对象的动作数组中移除即可。@H_403_15@
接下来看看@H_403_15@Node@H_403_15@中的@H_403_15@stopAction@H_403_15@函数:@H_403_15@
函数原理都是一样的,拿其中一个举例:
注意事项:不要用@H_403_15@action@H_403_15@中的@H_403_15@stop@H_403_15@函数来停止动作,因为判断动作是否结束的标志是@H_403_15@isDown@H_403_15@函数,并且判断过程还是在动作执行之后。@H_403_15@stop@H_403_15@函数会将@H_403_15@_target@H_403_15@置为@H_403_15@null@H_403_15@,这样运行动作就会试图去修改@H_403_15@_target@H_403_15@的属性,导致程序奔溃。还有一点就是动作运行结束后会被删除,如果想多次运行动作,请@H_403_15@retain@H_403_15@。@H_403_15@
总结:
动作最重要的几个函数
isDown
step
stop
update
以及@H_403_15@ActionManager::update@H_403_15@
还有动作的重要属性:
_target
_origintarget
_elapsed