我需要循环一个数字(xx)。xx 总是从零开始。我的问题是,如果moveDirection
变量为 +1,那么 xx 会增加,直到达到 的正数range
。如果moveDirection
为 -1,则 xx 减小直到达到 的负数range
。
在下面的代码中,我首先通过 if 语句测试 moveDirection 来做到这一点,然后我复制了 for 循环,并编辑了每种情况的值。我的代码恰好在 ActionScript3 中,但语言无关紧要。
var p:Point;
var xx:int;
if (moveDirection > 0)
{
for (xx = 0; xx < range; xx++)
{
if (hitTestPoint(xx, yy))
{
return true;
}
}
}
else
{
for (xx = 0; xx > range; xx--)
{
if (hitTestPoint(xx, yy))
{
return true;
}
}
}
有没有更好的方法可以做到这一点,也许不需要复制 for 循环?如果有任何其他建议,将不胜感激。