# Creating a grid of hexes using for loops

**URL:** https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/57260
**Category:** flash
**Created:** [July 11, 2004, 10:14pm UTC](https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/57260 "2004-07-11T22:14:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Boondogger](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@Boondogger](https://forum.kirupa.com/u/Boondogger)
#### Post date: [July 11, 2004, 10:14pm UTC](https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/57260/1 "2004-07-11T22:14:03Z")

</div>

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)

Boondogger

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2004, 10:18pm UTC](https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/57260/2 "2004-07-12T22:18:49Z")

</div>

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.

Boondogger
