Hello,

I've been doing some study on how to create a grid-like representation of
flat-based arrays.

Considering you are working with a data backend that will server you a
one-dimensional array like this:

var arr = [1,2,3,4,5,6,7,8,9,0]

And you would like to render in in your flash applications (or whatever)
like a grid/matrix:

1,2,3,4
5,6,7,8
9,0

The way I do it currently:

<as2 code>

//First I decide on a condition to when I will "reset the column pointer and
do a line break"

var MAX_COLS = 4;

for (i=0; i < arr.length; i++)
{
   if (i % MAX_COLS == 0 && i != 0)
    {
         rows++;
     }

    var thumb_mc.x = i*(thumb_mc.width) + offset;
    var thumb_mc-y = rows*(thumb_mc.height) + offset;

}

</as2 code>

Not that this is pseudo-code, I haven't actually tested it, but you can get
the idea.

I could have used a cols variable in the place of using the i counter and
just replace the condition with the modulus with a "if cols > MAX_COLS",
since I would be reseting (cols = 0) this var when this condition were true:

if (cols >= MAX_COLS)
{
  cols = 0;
  rows++
}

Now, what bothering me is the two dimensional array usually used in
tile-based games.

I was thinking this morning that maybe it would be easier to just convert
the flat-based array to a two dimensional one beforehand, like this:

var arr = [[1,2,3,4],[5,6,7,8],[9,0]];

and then use a loop like:

for (i = 0; i < arr.length; i ++) {
  for (j = 0; j < arr[i].lentgh; j++) {
     thumb_mc.x = i*thumb_mc.width + offset;
     thumb_mc.y = j*thumb_mc.height + offset;
  }
}

Please, correct me if I'm wrong on any of these points/code.

What do you think?

Thanks in advance,

PS.: Btw, is there a book or something that has recipes/patterns for such
problems ? (like:  ways to render data in  a grid-based fashion).

Marcelo.
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to