# Creating a grid of hexes using for loops

**URL:** https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/115781
**Category:** Uncategorized
**Created:** [July 11, 2004, 10:14pm UTC](https://forum.kirupa.com/t/creating-a-grid-of-hexes-using-for-loops/115781 "2004-07-11T22:14:03Z")
**Posts on this page:** 1
**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/115781/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
