我有这段代码
var draw = function() {
var bx = 0;
var by = 0;
//height
for (var i = 0; i < 11; i++) {
//width
for (var s = 0; s < 10; s++) {
block(bx,by,air);
bx = bx + 50;
}
by = by + 50;
}
当我运行draw()
时,它应该在10×10的整个部分绘制正方形。然而,这种情况并非如此。这一切都搞砸了,我不知道发生了什么。
注意:这是使用processing.js。您可以在此处找到完整的代码:https://www.khanacademy.org/computer-programming/mc-10/4727460304912384
答案 0 :(得分:3)
不要为迭代和绘图使用单独的变量。
var width = 50;
for (var by = 0; by < 11 * width; by += width) {
for (var bx = 0; bx < 10 * width; bx += width) {
block(bx, by, air);
}
}
或:
var width = 50;
for (var i = 0; i < 11; i++) {
for (var s = 0; s < 10; s++) {
block(s * width, i * width, air);
}
}
答案 1 :(得分:1)
您需要在每次迭代时重置bx
for (var i = 0; i < 11; i++) {
bx = 0;
for (var s = 0; s < 10; s++) {
block(bx,by,air);
bx = bx + 50;
}
by = by + 50;
}