PDA

View Full Version : Question about simple code and arrays


Roycommi
08-06-2003, 03:18 PM
Hi,

I am trying to figure out how to do tile based games so I am following the examples on outsideofsociety.idz.net .

At any rate, I am a bit confused at a bit in the first example: drawing a map. Here is the code:

fscommand ( "allowscale", false );

myMap = [ [1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,1],
[1,0,1,0,0,0,0,1],
[1,0,0,0,0,1,0,1],
[1,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1] ];

tileW = 24;
tileH = 24;
boarder = 001.bmp;

function buildMap (map) {
var mapWidth = map[0].length;
var mapHeight = map.length;
for (var L = 0; L<mapHeight; ++L) {
for (var w = 0; w<mapWidth; ++w) {
this.attachMovie("tile", "t_"+L+"_"+w, ++d);
this["t_"+L+"_"+w]._x = (w*tileW);
this["t_"+L+"_"+w]._y = (L*tileH);
this["t_"+L+"_"+w].gotoAndStop(map[L][w]+1);
}
}
}

buildMap (myMap);

I do not understand what mechanism here actually says "if array component (x,x) = 1 then paint it black, else paint it white"

I am new to Flash MX and actionscript so i imagine there is a fundamental part I am missing.

Thanks in Advance!!

Roycommi

senocular
08-06-2003, 03:27 PM
this["t_"+L+"_"+w].gotoAndStop(map[L][w]+1);


frame 1 is white, frame 2 is black

0+1 is 1
1+1 is 2

Roycommi
08-06-2003, 04:57 PM
Okay, I see what it says but i dont undferstand what its doing.

dzy2566
08-06-2003, 05:47 PM
this["t_"+L+"_"+w].gotoAndStop(map[L][w]+1);

It adds one to the value held at the "coordinate" map[L][w]. So in the example of the grid you have, map[0][0], which is the upper left tile, has a value of 1. So the code above, on the upper left tile, evaluates to 2, thus sending the tile in the upper left to frame 2, which is a picture of a black tile. map[1][1] has a value of 0. So when the above line is evaluated with map[1][1] you get 0 + 1, and the tile one in and one down goes to frame one, which has a pic of a white tile.

If there were more than 2 tile states, that line would have to be a little more complicated or might end up being an if/else statement. But in this case, you're seeing a clever way of turning the value in the multidimensional array into the actual frame of the tile.