1

次のコードがあります

for(i = 0; i < num; i++) {
    var yPos = 10*i;
    var numCells = wid/30;

    for(j = 0; j < numCells; j++) {
        blocks[i][j] = 1;
    }                       
}

blocks = new Array();

ただし、コードを実行すると、次のようなエラーが表示されます。

can't convert undefined to object

何か案は?:/

4

2 に答える 2

4
var blocks = [];
for(i = 0; i < num; i++) {    
    var yPos = 10*i;
    var numCells = wid/30;
    blocks[i] = []; // here is a fix

    for(j = 0; j < numCells; j++) {
        blocks[i][j] = 1;
    }
}
于 2012-12-09T13:30:11.100 に答える
0

In your particular case, since all the rows are initialised to be the same (a series of 1s), you can also do

var blocks = new Array(),
    blockRow = new Array();
for (var i = 0; i < numCells; i++) {
    blockRow.push(1);
}
for (var i = 0; i < num; i++) {
    blocks.push(blockRow.slice());  // slice() makes a copy of blockRow
}
于 2012-12-09T14:17:39.523 に答える