我想用JavaScript.I平铺一个对象网格,我已经忘记了这个公式,似乎在任何地方都找不到它:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
for ( j = 0; j < col; j ++ ) {
var object = new object();
object.position.x = 0 + width * j
// do i nee another loop here?
//add object to....
}这将返回按宽度间隔的10个对象的行,但我也希望列类似于10x10网格,就在JavaScript中的公式之后。

发布于 2015-04-25 15:07:14
您可以嵌套两个for循环,如下所示:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
var space = ...;
// Rows loop
for ( j = 0; j < row; j ++ ) {
// For each column in the row
for ( i = 0; i < col; i ++ ) {
// j is the row index and i is the column index..
var object = new object();
object.position.x = (width + space) * i;
object.position.y = (height + space) * j;
// Add object to...
}
}https://stackoverflow.com/questions/29866786
复制相似问题