这是我用来绘制一些随机圆圈的代码的一部分:
if(circles.length != 0) { //1+ circles have already been drawn
x = genX(radius);
y = genY(radius);
var i = 0;
iCantThinkOfAGoodLabelName:
for(i in circles) {
var thisCircle = circles[i];
if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) {
//overlaps
} else {
//overlaps
x = genX(radius);
y = genY(radius);
continue iCantThinkOfAGoodLabelName;
}
if(i == circles.length - 1) { //Last iteration
//Draw circle, add to array
}
}
}
问题在于,当存在重叠时,不检查具有新生成的坐标的圆与重叠圆已经被检查过的圆重叠。我尝试在使用continue语句之前将i设置为0,但这不起作用。请帮忙,我真的很困惑。
答案 0 :(得分:4)
You should not use for ... in
on arrays.
请改用for(var i = 0; i < circles.length; ++i)
。然后,您可以通过设置i = 0
。
答案 1 :(得分:1)
为什么不使用循环标准
for (var i=0,l = circles.length;i < l; i++) {
....
if (i === l) {
// draw
}
}
答案 2 :(得分:0)
我不完全理解这个问题,但我不相信你可以重置for..in
的迭代。您需要转到for(var i=0;...;i++)
。