engineer7
engineer7

Reputation: 1

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?

Upvotes: 0

Views: 250

Answers (2)

engineer7
engineer7

Reputation: 1

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.

Upvotes: 0

bipll
bipll

Reputation: 11940

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());
}

Upvotes: 0

Related Questions