Okay, I'm trying to load a 2d array and having some problems. Here's my code:
var blockSize = 30;
var level = new Array(new Array(0, 1, 0, 1, 0, 1, 0, 1, 0, 1), new Array(1, 0, 1, 0, 1, 0, 1, 0, 1, 0));
var blockArray = new Array(1);
blockArray[0] = new Array(1);
function readLevel() {
for (var i = 0; i < level.length; i++) {
for (var j = 0; j < level[i].length; j++) {
var tempImg = new Image();
tempImg.src = "images/block.png";
blockArray[i][j] = new block(i * blockSize, j * blockSize, level[i][j], false, false, tempImg);
//throw('blockArray['+i+']'+j+'] = ' + level[i][j]);
}
}
}
And here is my error:
Firebug's log limit has been reached. 0 entries not shown. Preferences
blockArray[i] is undefined
[Break On This Error] blockArray[i][j] = new block(i *...level[i][j], false, false, tempImg);
How do I fix this?
new Array(1)are you trying to create an empty 1-element array, or a 1-element array that contains1? Unless you want to create an empty,n-element array, there's no reason to use thenew Array()constructor; just use array literals instead. Ex., yournew Array(0, 1, 0, 1, 0, 1, 0, 1, 0, 1)call would simply become[0, 1, 0, 1, 0, 1, 0, 1, 0, 1].var level = [[0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]];