Creating a grid of hexes using for loops

I’m trying to create a grid of hexagons by using nested for loops. I picked up this code trawling the forums but this creates a diamond shaped pattern of hexes rather than neat rows that interlock together. I want to create one row in the first place, increment the x and y placing coords so as the next row is offset as appropriate to lock it into place under row above. Can’t get my head round it. It’s been a long day-too much wine-Flash newbie…You get the picture. Can anyone sort this out?

This is the code I’m using, which is clearly not doing the trick.

spacing = 35;
spacing2 = 35;
for (x = 0; x < 10; x++){
for (y = 0; y < 10; y++){
tile = this.attachMovie(“mytile”, “mytile” + depth, depth++);//attaches the tile
tile._x = spacing * (x + y);
tile._y = spacing2* (x - y);
tile.x = x;
tile.y = y;
tile.onRelease = function(){
trace("x: “+this.x+”, y: "+this.y);


Any help would be much appreciated.

(fla attached if you want to take a look)


Fixed it now anyway. What is weird is I did a lot of it through trial and error. Code below:

function LineHex () {//makes a row of hexes
for (i=0;i<20;i++) {
tile = this.attachMovie(“mytile”, “mytile” + depth, depth++);
if (j%2) {offset1=0;//if it’s odd start on edge of page
else{offset1=18;//if it’s even start in from edge of page
tile._x = (offset1 + spacing);//dist from edge + width of hex
tile._y = ((var1 + offset2)/30);//dist from top edge
tile.x = x-1;//label for hex
tile.y = y;//label for hex
spacing += 35;
tile.onRelease = function(){
trace("x: “+this.x+”, y: "+this.y);
}//end of for loop
}//end of function
for (j=0;j<20;j++) {//initiates a new row
spacing =0;//restores new row to it’s proper inset from left edge


I don’t understand the relationship between the 30 here "((var1 + offset2)/30);"
and the 880 here “var1+=880;”

It works and that’s the main thing, but I wish I understood why.