【发布时间】:2012-06-13 00:36:56
【问题描述】:
以下代码检查球是否与砖重叠。如果发生重叠,砖块会突出显示并从列表中删除。
在仅擦除矢量 brickWall 中的最后一项时,我遇到了核心转储。
注释 erase() 行,代码似乎运行良好。
在论坛上查看了类似的问题后,我相信我正在正确地迭代和擦除向量中的项目,但我怀疑情况可能并非如此。
对此代码的任何建议将不胜感激!
void Game::updateEntities()
{
ballMother.update();
for (std::vector<gdf::Entity>::iterator it = brickWall.begin(); it != brickWall.end(); ++it)
{
if (it->getRect().intersects(ballMother.getRect())) {
it->rect.setFillColor(sf::Color::Red);
it = brickWall.erase(it);
}
else {
it->rect.setFillColor(it->colour);
}
}
}
【问题讨论】:
-
您将 it 迭代器加倍。一次进入 for 循环,第二次进入擦除。