2 pointer23 pointer23 于 2014.09.16 14:06 提问

cocos2dx中关于addChild()引用计数具体位置

都说cocos2d中使用addChild()时引用计数加一,请问具体在哪儿?`void Node::addChild(Node *child, int zOrder, int tag)
{

CCASSERT( child != nullptr, "Argument must be non-nil");
CCASSERT( child->_parent == nullptr, "child already added. It can't be added again");

if (_children.empty())
{
    this->childrenAlloc();
}

this->insertChild(child, zOrder);

#if CC_USE_PHYSICS
if (child->getPhysicsBody() != nullptr)
{
child->getPhysicsBody()->setPosition(this->convertToWorldSpace(child->getPosition()));
}

for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
{
    if (dynamic_cast<Scene*>(node) != nullptr)
    {
        (dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
        break;
    }
}

#endif

child->_tag = tag;

child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);

if( _running )
{
    child->onEnter();
    // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
    if (_isTransitionFinished) {
        child->onEnterTransitionDidFinish();
    }
}

if (_cascadeColorEnabled)
{
    updateCascadeColor();
}

if (_cascadeOpacityEnabled)
{
    updateCascadeOpacity();
}

}
`

1个回答

pointer23
pointer23   2014.09.23 23:52
已采纳

进入this->insertChild(child, zOrder);
再进入
_children.pushBack(child);
就可以看到了
void pushBack(T object)
{
CCASSERT(object != nullptr, "The object should not be nullptr");
_data.push_back( object );
//addChild的引用计数加一在这
object->retain();
}``

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!