Ogre3d:迭代通过childnode错误 [英] Ogre3d: iterating through childnode error
问题描述
我有一个城市节点,其中包含许多建筑节点,我希望每个节点都有一个新的子节点。告诉房子他们有什么角色和标志/角色。以后可以用于其他功能。现在所有人都得到相同的.mesh(稍后会做出一个标志)来确定哪个房子是什么。它应随机分配。如果我尝试运行此操作,我会收到以下错误。我对食人魔很新,这增加了一些奇怪的代码
I have a city node which houses many building nodes, each of these I wish to grant a new child-node. which tells the house what role and sign they have/role. Which can later be used for other functions. for now all get the same .mesh (will later make a sign) to identify which house is what. it shall be randomly assigned. If I try to run this I get the following error. I am very new to ogre , which adds to some weird code
int CityManager::assignBuildingRole(Ogre::SceneNode * _cityNode, int _numberOfBuildings)
{
std::stringstream nodename("buildingRoleNode");
Ogre::SceneNode::ChildNodeIterator cNode = _cityNode->getChildIterator();
std::vector <Ogre::SceneNode*> detachable;
while (cNode.hasMoreElements()) {
detachable.push_back((Ogre::SceneNode *)cNode.getNext());
}
for (int i = 0; i < detachable.size(); i++) {
nodename << childIteration << "_" << parentIteration << "_" << i;
switch (rand() % 5) // assign building random proffessions by giving them a rolenode
{
case 0:
_roleNode = ( Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 1:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 2:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 3:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 4:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
default:
break;
}
}
return role;
}
推荐答案
致电上的> getNext
ChildNodeIterator 不仅会检索下一个元素,还会将 advance 检索到下一个元素。请参阅文档中的说明
Calling getNext
on ChildNodeIterator
will not only retrieve the next element but advance to the next element, too. See its description in documentation
所以你要做的是迭代 _cityNode
的孩子,并将每个孩子存放在可拆卸的$ c $中c> vector。
So what you do is iterating over the children of _cityNode
and store every child in your detachable
vector.
while (cNode.hasMoreElements()) {
detachable.push_back((Ogre::SceneNode *)cNode.getNext());
}
在这些行之后你的 cNode
指向子项结束列表(特别是在之后的最后一个元素, end()
在 std :: map中
这是底层数据类型)
After these lines your cNode
points to end of children list (specifically after the last element, as end()
in std::map
which is the underlying data type)
如果你想进一步迭代它,你会收到错误。
If you are trying to iterate it further you'll get an error.
您可以使用可拆卸
进行下一次迭代(我想这就是您创建它的原因)
You can either use detachable
for the next iteration (I guess that's why you created it)
_roleNode = detachable[i]->createChild(nodename.str());
或在while循环中创建子项,不要使用可分离
。
or do the child creation in your while loop and don't use detachable
at all.
while (cNode.hasMoreElements()) {
Ogre::SceneNode* curr = (Ogre::SceneNode *)cNode.getNext();
switch (rand() % 5)
{
case 0:
_roleNode = curr->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
/* ... */
default:
break;
}
}
这篇关于Ogre3d:迭代通过childnode错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!