我正在使用这段代码:
void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
while (itr != _gameObjects.end())
{
//if (itr->second->GetObjectType() == VisibleGameObject::HUD)
//{
itr->second->Draw(renderWindow);
itr++;
//}
}
}
绘制ObjectType HUD的所有元素。 ObjectType 是我的另一个类 VisibleGameObject 中的一个枚举。如果我在我的代码中使用斜杠(所以当我不检查枚举时)一切正常。但是当我只想绘制 ObjectType HUD 的对象时,我的程序就会卡在 Debug模式。我在调试或其他任何方面都没有收到任何错误。它只是停留在绘制整个事物上。
有人看到我做错了什么吗?
提前致谢
Xentro
最佳答案
您遇到的问题是,如果条件不为真,那么您永远不会递增迭代器。如果您从不递增迭代器,它永远不会等于 end()
。您需要始终递增迭代器并在您的 if 语句中进行处理:
void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
while (itr != _gameObjects.end())
{
if (itr->second->GetObjectType() == VisibleGameObject::HUD)
{
itr->second->Draw(renderWindow);
}
itr++; // unconditional increment.
}
}
如果您愿意,您可以使用 C++11 及更高版本的基于范围的 for 循环,例如:
void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
for (auto & e : _gameObjects)
{
if (e.second->GetObjectType() == VisibleGameObject::HUD)
e.second->Draw(renderWindow);
}
}
关于c++ - 来自 C++ 中另一个类的枚举(使用 SFML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012359/