我正在爲我的pcg遊戲進行城市生成。我有一個循環,這使得3個城市的隨機位置,我給你parentIteration
得到那個「ID」爲城市做同樣的for
圈,我做出一個建築Ogre3d具有獨特的節點名稱錯誤
for (int i = 0; i < 3; i++)
{
parentIteration = i;
std::srand(i);
_rootNode = GameManager::getSingletonPtr()->getSceneManager()->getRootSceneNode();
_cityNode = _rootNode->createChildSceneNode("cityNode " + parentIteration);
generateCity(std::rand() % 10000 + 10, std::rand() % 10000 + 10, std::rand() % 11 +1);
}
建設
for (int i = 0; i < _numberOfBuildings; i++)
{
childIteration = i;
printf(" parent %d and child %d \n", parentIteration, childIteration);
Ogre::SceneNode* buildingNode = _cityNode->createChildSceneNode("citybuildingNode"+childIteration+parentIteration);
}
但是,當我嘗試啓動遊戲時,它會在創建第二個城市時崩潰。說它已經有一個類似於它試圖寫的名字。然而我的printf
清楚地表明,那時的數字都是獨一無二的。任何人都知道如何解決這個問題? (輸出證明添加圖片)
你確定''citybuildingNode「+ childIteration + parentIteration'是否符合你的想法?因爲我認爲你打算做一個字符串連接而不是指針算術。 'std :: string(「citybuildingNode」)+ childIteration + parentIteration'似乎更合適 – PeterT
'cityNode = _rootNode-> createChildSceneNode(「cityNode」+ parentIteration);' - 我不知道'Ogre',重新傳球 - 預期的實際類型是什麼?你正在添加一個字符串文字和一個「int」。這不會像預期的那樣工作,除非有一些我不知道的魔法。 – PaulMcKenzie
[可能的重複](http://stackoverflow.com/questions/191757/how-to-concatenate-a-stdstring-and-an-int)。這可能是你的問題歸結爲什麼。 C++不像其他語言那樣使用字符串文字。你不能只是將一個數字連接到一個文字上,並得到一個你期望的字符串。 – PaulMcKenzie