[SOLVED] Using setTileAt to translate Array into Map

tristonhopkins

  • Posts: 69
I have yet to figure out the "random" part of the map generation, but I was able to generate a blank map with default tiles into an array.  My particular problem is getting Stencyl to translate that array into graphics.  I know how setTileAt works, but how to call it for each entry in the array completely alludes me.  I've seen how to do it with Flixel but I can't get Stencyl to do the same thing because of the difference in API.  If anyone has any relevant code I could look at, that would help immensely as well.  I just need any help I can get right now! :P

« Last Edit: May 07, 2012, 01:41:30 pm by tristonhopkins »

Ryusui

  • Posts: 827
Just iterate over the array with a "for" loop.
In the event of a firestorm, the salad bar will remain open.

tristonhopkins

  • Posts: 69
Ah, I think I didn't explain myself correctly.  I didn't mean how do I call setTileAt for every X amount of entries in the array, but rather how do I tell setTileAt where each tile goes.

Ryusui

  • Posts: 827
How's your Array set up? If you've got a two-dimensional array, it's easy: just use nested "for" loops and use the iterators for both the array values and the X/Y coordinates of the map.

I don't remember the specifics, but it should look something like...

Code: [Select]
for(var yValue=0;yValue<mapHeight-1;yValue++){
for(var xValue=0;xValue<mapWidth-1;xValue++){
setTileAt(xValue,yValue,myMap[xValue][yValue]);
}
}

I think setTileAt goes "row" then "column", so you might have to switch "xValue" and "yValue" in this example.

If it's a one-dimensional array, it's a little more complicated, but...

Code: [Select]
for(var pos=0;pos<myMap.length-1;pos++){
setTileAt(pos%mapHeight,Math.floor(pos/mapHeight),myMap[pos]);
}

That is, you'd take the mod of the iterator and the map's height to get the X coordinate, and round down the iterator divided by the map's height to get the Y coordinate. For example, if your map is 10 tiles high, then the value at pos 14 has an X of 4 (14 mod 10) and a Y of 1 (14/10 rounded down = 1).
In the event of a firestorm, the salad bar will remain open.

tristonhopkins

  • Posts: 69
Thanks!  I was able to figure it out.

Now I just need to wrap my head around randomly generating perlin noise and then how to output it into the array I use for my terrain map.  :'(