0

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?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
CyanPrime
  • 5,096
  • 12
  • 58
  • 79
  • 2
    When you write `new Array(1)` are you trying to create an empty 1-element array, or a 1-element array that contains `1`? Unless you want to create an empty, `n`-element array, there's no reason to use the `new Array()` constructor; just use array literals instead. Ex., your `new 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]`. – Matt Ball Jan 23 '11 at 15:24
  • 2
    Better: `var level = [[0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]];` – Šime Vidas Jan 23 '11 at 15:26

2 Answers2

5

Your outer loop, on i, will go from 0..1 (inclusive). You're never setting blockArray[1] to anything, and so it's undefined. So your blockArray[i][j] = ... line will fail because you can't index into an undefined value (blockArray[i] is undefined when i = 1).

I'm not entirely sure what your code is meant to do, but this should get you close, anyway:

var blockSize = 30;

var level = [
                [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
            ];

var blockArray = [];

function readLevel() {
    for (var i = 0; i < level.length; i++) {
        blockArray[i] = []; // Create the second level for this index
        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]);
        }
    }
}

Note that I've also ditched the new Array(n) syntax in favor of array literal syntax. I think it's clearer for what you're doing here.

It's important to realize that JavaScript doesn't really have arrays, and it certainly doesn't have 2D arrays. JavaScript "arrays" are really just objects that give special treatment to a class of property names (ones consisting entirely of digits) and that have a special length property. (And, of course, they have all the functions they inherit from Array.prototype.) More here.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Try to add between your two for-loops these line

blockArray[i] = new Array();

to create the second level array.

function readLevel() {
    for (var i = 0; i < level.length; i++) {
        blockArray[i] = new Array();
        for (var j = 0; j < level[i].length; j++) {
            [etc]
        }
    }
}
Frederik Kammer
  • 3,117
  • 3
  • 28
  • 29
  • Or `blockArray[i] = [];` Why do you guys keep using this other notation? `[]` is clearly better. – Šime Vidas Jan 23 '11 at 15:31
  • 1
    [See this Q & A.](http://stackoverflow.com/questions/1936047/jslint-use-the-array-literal-notation-for-var-os-map) – user113716 Jan 23 '11 at 15:38
  • 1
    It's more terse and readable, especially for nested arrays. Also, there is this issue with the number and type of arguments: `new Array(3)` and `new Array(3, 3)` and new `Array('3')` generate unexpectedly different results `[]` and `[3, 3]` and `['3']` respectively. Professional JavaScript programmers avoid the `new Array()` notation. – Šime Vidas Jan 23 '11 at 15:48