0

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?

engineer7
  • 1
  • 1

2 Answers2

0

Are those nodes simply clonable? If yes, you can add an identical copy at each iteration:

auto nodeToAdd = loadCcbAsNode("fileccb.ccbi").get();
listView->addChild(nodeToAdd);

for(size_t i = 1; i < 9; i++) {
    listView->addChild(nodeToAdd->clone());
}

Or, alternatively, if it's a one-time action, load a few instances on the go:

for(size_t i = 1; i < 10; i++) {
    listView->addChild(loadCcbAsNode("fileccb.ccbi").get());
}
bipll
  • 11,747
  • 1
  • 18
  • 32
  • cocos2d::node haven't method clone, and 2 variant adding just one node to list. it's not helped – engineer7 Mar 01 '18 at 15:30
  • Okay, try to use `new cocos2d::node(*nodeToAdd)` instead of `->clone()` it lacks. – bipll Mar 01 '18 at 15:40
  • Concerning variant 2, in your own response you're doing practically the same but setting position. Is it ListView does not automatically handle its nodes' positions and you have actually seen nine identical nodes on top of one another? – bipll Mar 01 '18 at 17:33
0
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.

engineer7
  • 1
  • 1
  • Will it work if you move `NodePtr nodeToAdd = loadCcbAsNode("fileccb.ccbi");` out of the loop? – bipll Mar 01 '18 at 15:43
  • Don't work. Also call assert `CCASSERT(child->_parent == nullptr, "child already added. It can't be added again");` – engineer7 Mar 01 '18 at 16:01