概述
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;
}
}
}
原因引起:
如果场景更新成功后,修改了判断条件的一个变量townLastUpdateTick_(或ieldLastUpdateTick_ ),会导致循环后面的本来成功的判断都会失败。
谨记这种因为错误的更改了 循环内的判断条件的值 引起的bug。
最后
以上就是强健大神为你收集整理的切记循环内修改判断条件要谨慎的全部内容,希望文章能够帮你解决切记循环内修改判断条件要谨慎所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复