bug现象:我更改了场景更新时间,将原来一个场景的更新时间改成了3种不同类型分别不同的更新时间,然后场景里面的怪物就不运动了。
原代码:
time_t diff = curTick - lastUpdateTick_;
//std::cout<<" diff:"<<diff<<std::endl;
if (diff > 500)
diff = 500;
lastUpdateTick_ = curTick;
for (ScenarioMap::const_iterator it = scenarioMap_.begin(); it != scenarioMap_.end(); it++)
{
it->second->update(diff);
}
修改后代码:
for (ScenarioMap::const_iterator it = scenarioMap_.begin(); it != scenarioMap_.end(); it++)
{
Scenario* scene = it->second;
if(scene->isTown())
{
time_t diff = curTick - townLastUpdateTick_;
if(diff > townUpdateDiff)
{
scene->update(diff);
townLastUpdateTick_ = curTick;
}
}
if(scene->isField())
{
time_t diff = curTick - fieldLastUpdateTick_;
if(diff > fieldUpdateDiff)
{
scene->update(diff);
fieldLastUpdateTick_ = curTick;
}
}
}
原因引起:
原代码:
time_t diff = curTick - lastUpdateTick_;
//std::cout<<" diff:"<<diff<<std::endl;
if (diff > 500)
diff = 500;
lastUpdateTick_ = curTick;
for (ScenarioMap::const_iterator it = scenarioMap_.begin(); it != scenarioMap_.end(); it++)
{
it->second->update(diff);
}
修改后代码:
for (ScenarioMap::const_iterator it = scenarioMap_.begin(); it != scenarioMap_.end(); it++)
{
Scenario* scene = it->second;
if(scene->isTown())
{
time_t diff = curTick - townLastUpdateTick_;
if(diff > townUpdateDiff)
{
scene->update(diff);
townLastUpdateTick_ = curTick;
}
}
if(scene->isField())
{
time_t diff = curTick - fieldLastUpdateTick_;
if(diff > fieldUpdateDiff)
{
scene->update(diff);
fieldLastUpdateTick_ = curTick;
}
}
}
原因引起:
如果场景更新成功后,修改了判断条件的一个变量townLastUpdateTick_(或ieldLastUpdateTick_ ),会导致循环后面的本来成功的判断都会失败。
谨记这种因为错误的更改了 循环内的判断条件的值 引起的bug。