2010-11-13 11:34:49 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:34:26 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Valentin Milea
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 15:08:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
CopyRight (c) 2013-2014 Chukong Technologies Inc.
|
2011-03-19 10:34:26 +08:00
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionManager.h"
|
|
|
|
#include "2d/CCNode.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "base/ccMacros.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/ccCArray.h"
|
|
|
|
#include "2d/uthash.h"
|
2010-11-13 11:34:49 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-05 14:37:36 +08:00
|
|
|
//
|
|
|
|
// singleton stuff
|
|
|
|
//
|
2010-11-13 11:34:49 +08:00
|
|
|
typedef struct _hashElement
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
struct _ccArray *actions;
|
2013-12-06 18:16:58 +08:00
|
|
|
Node *target;
|
2013-09-08 11:26:38 +08:00
|
|
|
int actionIndex;
|
2013-06-20 14:13:12 +08:00
|
|
|
Action *currentAction;
|
2012-04-19 14:35:52 +08:00
|
|
|
bool currentActionSalvaged;
|
|
|
|
bool paused;
|
|
|
|
UT_hash_handle hh;
|
2010-09-04 11:48:11 +08:00
|
|
|
} tHashElement;
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionManager::ActionManager(void)
|
2013-12-18 17:47:20 +08:00
|
|
|
: _targets(nullptr),
|
|
|
|
_currentTarget(nullptr),
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTargetSalvaged(false)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionManager::~ActionManager(void)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing ActionManager: %p", this);
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
removeAllActions();
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
void ActionManager::deleteHashElement(tHashElement *element)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
ccArrayFree(element->actions);
|
|
|
|
HASH_DEL(_targets, element);
|
|
|
|
element->target->release();
|
|
|
|
free(element);
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
void ActionManager::actionAllocWithHashElement(tHashElement *element)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// 4 actions per Node by default
|
2013-12-18 17:47:20 +08:00
|
|
|
if (element->actions == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->actions = ccArrayNew(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
}else
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element->actions->num == element->actions->max)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
ccArrayDoubleCapacity(element->actions);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
void ActionManager::removeActionAtIndex(ssize_t index, tHashElement *element)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
Action *action = (Action*)element->actions->arr[index];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (action == element->currentAction && (! element->currentActionSalvaged))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->currentAction->retain();
|
|
|
|
element->currentActionSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(element->actions, index, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update actionIndex in case we are in tick. looping over the actions
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element->actionIndex >= index)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->actionIndex--;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element->actions->num == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
if (_currentTarget == element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTargetSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
deleteHashElement(element);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// pause / resume
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::pauseTarget(Node *target)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->paused = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::resumeTarget(Node *target)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->paused = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
Vector<Node*> ActionManager::pauseAllRunningActions()
|
2012-06-08 13:55:28 +08:00
|
|
|
{
|
2013-12-06 18:16:58 +08:00
|
|
|
Vector<Node*> idsWithActions;
|
2012-06-08 13:55:28 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
for (tHashElement *element=_targets; element != nullptr; element = (tHashElement *)element->hh.next)
|
2012-06-08 13:55:28 +08:00
|
|
|
{
|
|
|
|
if (! element->paused)
|
|
|
|
{
|
|
|
|
element->paused = true;
|
2013-12-06 18:16:58 +08:00
|
|
|
idsWithActions.pushBack(element->target);
|
2012-06-08 13:55:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 16:06:05 +08:00
|
|
|
return idsWithActions;
|
2012-06-08 13:55:28 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::resumeTargets(const Vector<Node*>& targetsToResume)
|
|
|
|
{
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &node : targetsToResume) {
|
2013-12-06 18:16:58 +08:00
|
|
|
this->resumeTarget(node);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-06-08 13:55:28 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 14:37:36 +08:00
|
|
|
// run
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
void ActionManager::addAction(Action *action, Node *target, bool paused)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT(action != nullptr, "");
|
|
|
|
CCASSERT(target != nullptr, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2014-02-20 10:53:49 +08:00
|
|
|
// we should convert it to Ref*, because we save it as Ref*
|
|
|
|
Ref *tmp = target;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &tmp, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (! element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element = (tHashElement*)calloc(sizeof(*element), 1);
|
|
|
|
element->paused = paused;
|
2013-07-18 07:56:19 +08:00
|
|
|
target->retain();
|
2013-11-05 08:30:33 +08:00
|
|
|
element->target = target;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_ADD_PTR(_targets, target, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
actionAllocWithHashElement(element);
|
2010-11-13 11:34:49 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
CCASSERT(! ccArrayContainsObject(element->actions, action), "");
|
|
|
|
ccArrayAppendObject(element->actions, action);
|
2010-11-13 11:34:49 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
action->startWithTarget(target);
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::removeAllActions()
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
for (tHashElement *element = _targets; element != nullptr; )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-06 18:16:58 +08:00
|
|
|
auto target = element->target;
|
2013-11-05 08:30:33 +08:00
|
|
|
element = (tHashElement*)element->hh.next;
|
2013-07-18 07:56:19 +08:00
|
|
|
removeAllActionsFromTarget(target);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::removeAllActionsFromTarget(Node *target)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit null handling
|
2013-12-18 17:47:20 +08:00
|
|
|
if (target == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
if (ccArrayContainsObject(element->actions, element->currentAction) && (! element->currentActionSalvaged))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
element->currentAction->retain();
|
|
|
|
element->currentActionSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
ccArrayRemoveAllObjects(element->actions);
|
|
|
|
if (_currentTarget == element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTargetSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
deleteHashElement(element);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// CCLOG("cocos2d: removeAllActionsFromTarget: Target not found");
|
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
void ActionManager::removeAction(Action *action)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit null handling
|
2013-12-18 17:47:20 +08:00
|
|
|
if (action == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *target = action->getOriginalTarget();
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
auto i = ccArrayGetIndexOfObject(element->actions, action);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (i != CC_INVALID_INDEX)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
removeActionAtIndex(i, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: removeAction: Target not found");
|
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
void ActionManager::removeActionByTag(int tag, Node *target)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-08-29 03:42:25 +08:00
|
|
|
CCASSERT(tag != Action::INVALID_TAG, "");
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT(target != nullptr, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
auto limit = element->actions->num;
|
|
|
|
for (int i = 0; i < limit; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
Action *action = (Action*)element->actions->arr[i];
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (action->getTag() == (int)tag && action->getOriginalTarget() == target)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
removeActionAtIndex(i, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
// XXX: Passing "const O *" instead of "const O&" because HASH_FIND_IT requries the address of a pointer
|
|
|
|
// and, it is not possible to get the address of a reference
|
2013-12-06 18:16:58 +08:00
|
|
|
Action* ActionManager::getActionByTag(int tag, const Node *target) const
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-08-29 03:42:25 +08:00
|
|
|
CCASSERT(tag != Action::INVALID_TAG, "");
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (element->actions != nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
auto limit = element->actions->num;
|
|
|
|
for (int i = 0; i < limit; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
Action *action = (Action*)element->actions->arr[i];
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
if (action->getTag() == (int)tag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
return action;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-28 03:15:00 +08:00
|
|
|
CCLOG("cocos2d : getActionByTag(tag = %d): Action not found", tag);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
// CCLOG("cocos2d : getActionByTag: Target not found");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-05 14:37:36 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
// XXX: Passing "const O *" instead of "const O&" because HASH_FIND_IT requries the address of a pointer
|
|
|
|
// and, it is not possible to get the address of a reference
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t ActionManager::getNumberOfRunningActionsInTarget(const Node *target) const
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tHashElement *element = nullptr;
|
2013-11-11 12:47:48 +08:00
|
|
|
HASH_FIND_PTR(_targets, &target, element);
|
2013-11-05 08:30:33 +08:00
|
|
|
if (element)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-05 08:30:33 +08:00
|
|
|
return element->actions ? element->actions->num : 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// main loop
|
2013-06-20 14:13:12 +08:00
|
|
|
void ActionManager::update(float dt)
|
2010-08-05 14:37:36 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
for (tHashElement *elt = _targets; elt != nullptr; )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget = elt;
|
|
|
|
_currentTargetSalvaged = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _currentTarget->paused)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
// The 'actions' MutableArray may change while inside this loop.
|
2013-06-15 14:03:30 +08:00
|
|
|
for (_currentTarget->actionIndex = 0; _currentTarget->actionIndex < _currentTarget->actions->num;
|
|
|
|
_currentTarget->actionIndex++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_currentTarget->currentAction = (Action*)_currentTarget->actions->arr[_currentTarget->actionIndex];
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_currentTarget->currentAction == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget->currentActionSalvaged = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget->currentAction->step(dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTarget->currentActionSalvaged)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// The currentAction told the node to remove it. To prevent the action from
|
|
|
|
// accidentally deallocating itself before finishing its step, we retained
|
|
|
|
// it. Now that step is done, it's safe to release it.
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget->currentAction->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
} else
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTarget->currentAction->isDone())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget->currentAction->stop();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-05 08:30:33 +08:00
|
|
|
Action *action = _currentTarget->currentAction;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Make currentAction nil to prevent removeAction from salvaging it.
|
2013-12-18 17:47:20 +08:00
|
|
|
_currentTarget->currentAction = nullptr;
|
2013-11-05 08:30:33 +08:00
|
|
|
removeAction(action);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
_currentTarget->currentAction = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 no actions were scheduled during the cycle (issue #481)
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTargetSalvaged && _currentTarget->actions->num == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
deleteHashElement(_currentTarget);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// issue #635
|
2013-12-18 17:47:20 +08:00
|
|
|
_currentTarget = nullptr;
|
2010-08-05 14:37:36 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|