Search code examples
c++cocos2d-xcocosbuilder

How to add multiple identical nodes to ListView


I try add Node to ListView so:

auto nodeToAdd = loadCcbAsNode("fileccb.ccbi").get();

for (size_t i = 1; i < 10; i++)
{
    listView->addChild(nodeToAdd);       // it's cocos2d::ui::ListView
                                         // which i load to scene
}

But get so error:

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

What i need do?


Solution

  • for (int i = 1; i < 100; i++)
    {
        NodePtr nodeToAdd = loadCcbAsNode("fileccb.ccbi");
        nodeToAdd->setPosition(0,i*45);                      // 45-size of height my node
                                                             // need to bias
        listView->addChild(nodeToAdd.get());
    }
    

    as it turned out, when i create auto nodeToAdd = loadCcbAsNode("fileccb.ccbi").get();, nodeToAdd has a type Node, but cocos has special smart pointer NodePtr, and which i calling value with type of NodePtr, it's pointer automatically increments the counter by one, and Node this does not.