gpt4 book ai didi

javascript - 在 for...in 循环中重置迭代器

转载 作者:行者123 更新时间:2023-11-30 09:04:24 24 4
gpt4 key购买 nike

这是我用来绘制一些随机圆圈的代码的一部分:

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,但这没有用。请帮忙,我真的很困惑。

最佳答案

You should not use for ... in on arrays.

使用 for(var i = 0; i < circles.length; ++i)反而。然后你可以通过设置i = 0来重置.

关于javascript - 在 for...in 循环中重置迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6393876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com