I am writing a small "pacman" game in js. I am using html5 and canvas for it and it is going good. But I want to draw a map from a specification running on a multi-dimensional array. When I used a normal array it works like a charm but as soon as I use my multi dimensional array it just alerts "somthings wrong" which is sad.
I think it's because of this chunk:
function getMapArray() {
// define the yvalue of the map
// will be ytile later..
var items = new Array(1);
for(var i = 0; i < 2; i++) {
items[i] = new Array(xtile);
}
items[0][0] = "0010";
items[0][1] = "0010";
items[0][2] = "0010";
items[0][3] = "0010";
items[0][4] = "0010";
items[0][5] = "0010";
items[0][6] = "0010";
items[0][7] = "0010";
items[0][8] = "0010";
items[0][9] = "0010";
items[0][10] = "0010";
items[0][11] = "0010";
items[0][12] = "0010";
items[0][13] = "0010";
items[0][14] = "0010";
return items;
}
But I am not sure. It's probably somethings easy but I've been staring on the code for a long time and it says xtile is undefined even if I switch it with a integer.
Link to full source: http://pastie.org/3168446
JsFiddle demo: http://jsfiddle.net/928wU/
Thanks for helping!
[]
instead ofnew Array(x)
? Avoid usingnew Array
, there is no need to initialize the length of the array beforehand. And avoid globals.