源码:
//看看runAction
CCAction * CCNode::runAction(CCAction* action)
{
CCAssert( action != NULL, "Argument must be non-nil");
m_pActionManager->addAction(action, this, !m_bRunning); //往ActionManager加入了该动作
return action;
}
//看看ActionManager是做什么.....
void CCActionManager::addAction(CCAction *pAction, CCNode *pTarget, bool paused)
{
CCAssert(pAction != NULL, "");
CCAssert(pTarget != NULL, "");
tHashElement *pElement = NULL;
// we should convert it to CCObject*, because we save it as CCObject*
CCObject *tmp = pTarget;
HASH_FIND_INT(m_pTargets, &tmp, pElement);
if (! pElement)
{
pElement = (tHashElement*)calloc(sizeof(*pElement), 1);
pElement->paused = paused;
pTarget->retain();
pElement->target = pTarget;
HASH_ADD_INT(m_pTargets, target, pElement);
}
actionAllocWithHashElement(pElement);
CCAssert(! ccArrayContainsObject(pElement->actions, pAction), "");
ccArrayAppendObject(pElement->actions, pAction);
pAction->startWithTarget(pTarget); //执行动作
}
只要runAction 马上就会执行该动作, 与其他动作同时执行, 所以可能会产生预想不到的效果, 建议runAction前最好是stopAction先.
//这段代码可以看出, moveBy2先执行完,而moveBy其实也是同时执行的(比较隐形)
CCActionInterval *moveBy = CCMoveBy::create(20, ccp(-size.width/2,-size.height/2));
CCActionInterval *moveBy2 = CCMoveBy::create(2, ccp(size.width/2,size.height/2));
CCSprite *spx = CCSprite::create("HelloWorld.png");
spx->setPosition(CCPointZero);
spx->runAction(moveBy);
spx->runAction(moveBy2);
this->addChild(spx);
//修改了moveBy时间, 发现图片不动, 说明runAction可以同时执行多个动作
CCActionInterval *moveBy = CCMoveBy::create(2, ccp(-size.width/2,-size.height/2));
CCActionInterval *moveBy2 = CCMoveBy::create(2, ccp(size.width/2,size.height/2));
CCSprite *spx = CCSprite::create("HelloWorld.png");
spx->setPosition(CCPointZero);
spx->runAction(moveBy);
spx->runAction(moveBy2);
this->addChild(spx);