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.
Fixed it now anyway. What is weird is I did a lot of it through trial and error. Code below:
var1=0;
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
offset2=0;
}
else{offset1=18;//if it’s even start in from edge of page
offset2=30;
}
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
y++;
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
var1+=880;
y=0;
x++;
LineHex();
}
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.