Reputation: 4702
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!
Upvotes: 0
Views: 918
Reputation: 664207
Your getMap
doesn't throw an error. It returns an Array of length 2. The first element is an Array of length max(xTile, 15), the first 15 indexes filled with all the same string. The second element is an Array of length xTile, every index is empty (undefined). I do not know whether this is desired or not.
But now, look at the code you've posted here:
function drawMap(map) {
//draw the x
for(i = 0; i < map.length; i++) {
entry = map[i];
for(j = 0; j < entry.length; j++) {
if(i == 0 && j == 0)
fillCanvas(map[x], x, x+40, 40, 40);
else
fillCanvas(map[x], x*40, i*40, (x + 1) * 40, i*40);
}
}
}
This runs through your twodimensional array. But then, it tries to receive the x
property of your map. Wasn't x
the width of your canvas???
Upvotes: 1
Reputation: 324610
Try using var items = [];
(and similar for the inside ones) instead of new Array
.
If you can get the actual error message, that would be helpful.
Upvotes: 1