Action模块非常适合游戏对象的复杂操作
class CC_DLL Action : public Ref, public Clonable
class CC_DLL FiniteTimeAction : public Action
class CC_DLL ActionInterval : public FiniteTimeAction
class CC_DLL ActionInstant : public FiniteTimeAction
添加动作
Action * Node::runAction(Action* action){ CCASSERT( action != nullptr, "Argument must be non-nil"); _actionManager->addAction(action, this, !_running); return action;}
void ActionManager::addAction(Action *action, Node *target, bool paused){ element = (tHashElement*)calloc(sizeof(*element), 1); element->paused = paused; target->retain(); element->target = target; HASH_ADD_PTR(_targets, target, element); actionAllocWithHashElement(element); ccArrayAppendObject(element->actions, action); action->startWithTarget(target); // ??}
循环来源
void Director::drawScene()
void Scheduler::update(float dt)
void ActionManager::update(float dt)
遍历并调用 void Action::step(float dt)
void Action::update(float time) // 归一的时间参数
void Blink::update(float time){ if (_target && ! isDone()) { float slice = 1.0f / _times; float m = fmodf(time, slice); _target->setVisible(m > slice / 2 ? true : false); }}
很明晰对关系,也很方便添加自定义action
动作运行结束或者主动stop,ActionManager 会自动release对应的action
节点和action之间没有引用关系,都是通过ActionManager联系
Action * Node::getActionByTag(int tag){ CCASSERT( tag != Action::INVALID_TAG, "Invalid tag"); return _actionManager->getActionByTag(tag, this);}
this->runAction(MoveBy::create(4, ccp(100,0))); this->runAction(MoveBy::create(4, ccp(0,100)));
如果打开了CC_ENABLE_STACKABLE_ACTIONS宏(默认打开(3.0))
部分动作可以直接并行执行,例如MoveBy可以实现类似 物理学 合力的表现