Finding adjacent cells in an hex map
Filed Under Flash, Game design, Users contributions • 9 Comments
This tutorial continues Hex maps creation and rollover.
Now I will show you how find adjacent cells. Again, at the moment it's only code, and it only works for horizontal hex maps
-
hexagon_width = 38;
-
hexagon_height = 44;
-
grid_x_size = 12;
-
grid_y_size = 10;
-
sector_width = hexagon_width;
-
sector_height = hexagon_height/4*3;
-
gradient = (hexagon_height/4)/(hexagon_width/2);
-
for (x=0; x<grid_x_size; x++) {
-
for (y=0; y<grid_y_size; y++) {
-
hexagon_x_position = hexagon_width*x+(y%2)*hexagon_width/2;
-
hexagon_y_position = hexagon_height*y/4*3;
-
hexagon_number = x+y*grid_x_size;
-
hexagon_movieclip = attachMovie("hhexagon", "hhexagon_"+hexagon_number, hexagon_number, {_x:hexagon_x_position, _y:hexagon_y_position});
-
hexagon_movieclip.gotoAndStop(1);
-
hexagon_movieclip.txt.text = hexagon_number;
-
}
-
}
-
onEnterFrame = function () {
-
_root["hhexagon_"+hexagon_hover].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover+1)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover-1)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover+11)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover+13)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover-11)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(1);
-
_root["hhexagon_"+(hexagon_hover-13)].gotoAndStop(1);
-
sector_x = Math.floor(_xmouse/sector_width);
-
sector_y = Math.floor(_ymouse/sector_height);
-
delta_sector_x = _xmouse%sector_width;
-
delta_sector_y = _ymouse%sector_height;
-
switch (sector_y%2) {
-
case 1 :
-
if (delta_sector_x>=hexagon_width/2) {
-
if (delta_sector_y<(hexagon_height/2-delta_sector_x*gradient)) {
-
real_x = sector_x;
-
real_y = sector_y-1;
-
} else {
-
real_x = sector_x;
-
real_y = sector_y;
-
}
-
} else {
-
if (delta_sector_y<delta_sector_x*gradient) {
-
real_x = sector_x;
-
real_y = sector_y-1;
-
} else {
-
real_x = sector_x-1;
-
real_y = sector_y;
-
}
-
}
-
break;
-
case 0 :
-
real_x = sector_x;
-
real_y = sector_y;
-
if (delta_sector_y<((hexagon_height/4)-delta_sector_x*gradient)) {
-
real_x = sector_x-1;
-
real_y = sector_y-1;
-
}
-
if (delta_sector_y<((-hexagon_height/4)+delta_sector_x*gradient)) {
-
real_x = sector_x;
-
real_y = sector_y-1;
-
}
-
break;
-
}
-
if ((real_x>=0) and (real_x<grid_x_size) and (real_y>=0) and (real_y<grid_y_size)) {
-
hexagon_hover = real_x+real_y*grid_x_size;
-
_root["hhexagon_"+hexagon_hover].gotoAndStop(2);
-
if (hexagon_hover%grid_x_size != grid_x_size-1) {
-
_root["hhexagon_"+(hexagon_hover+1)].gotoAndStop(3);
-
}
-
if (hexagon_hover%grid_x_size != 0) {
-
_root["hhexagon_"+(hexagon_hover-1)].gotoAndStop(3);
-
}
-
if (Math.floor(hexagon_hover/grid_x_size)%2 == 0) {
-
if (hexagon_hover%grid_x_size != 0) {
-
_root["hhexagon_"+(hexagon_hover+11)].gotoAndStop(3);
-
_root["hhexagon_"+(hexagon_hover-13)].gotoAndStop(3);
-
}
-
_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(3);
-
_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(3);
-
} else {
-
if (hexagon_hover%grid_x_size != grid_x_size-1) {
-
_root["hhexagon_"+(hexagon_hover+13)].gotoAndStop(3);
-
_root["hhexagon_"+(hexagon_hover-11)].gotoAndStop(3);
-
}
-
_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(3);
-
_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(3);
-
}
-
}
-
};
and this is the result:
While I am optimizing the code and writing the routines for the vertical hex maps, James Prankard sent me the AS3 version of the code published in the Hex maps creation and rollover tutorial
Here it is:
-
// Container so you can easily getChildAt
-
var hexagon_container:MovieClip = new MovieClip();
-
addChild(hexagon_container);
-
// Declare variables
-
var hexagon_width:int = 38;
-
var hexagon_height:int = 44;
-
var hexagon_number = 0;
-
var grid_x_size:int = 12;
-
var grid_y_size:int = 10;
-
var sector_width:Number = hexagon_width;
-
var sector_height:Number = hexagon_height/4*3;
-
var gradient:Number = (hexagon_height/4)/(hexagon_width/2)
-
var hexagon_hover:Number = 0;
-
// Changed the y and x for loop to add the hexagons in the correct way for getChildAt
-
for (var y_pos:int =0; y_pos<grid_y_size; y_pos++) {
-
for (var x_pos:int =0; x_pos<grid_x_size; x_pos++) {
-
var hexagon_x_position:Number = hexagon_width*x_pos+(y_pos%2)*hexagon_width/2;
-
var hexagon_y_position:Number = hexagon_height*y_pos/4*3;
-
hexagon_number = x_pos+y_pos*grid_x_size;
-
var hexagon:hhexagon = new hhexagon();
-
hexagon_container.addChild(hexagon);
-
hexagon.x = hexagon_x_position;
-
hexagon.y = hexagon_y_position;
-
hexagon.txt.text = hexagon_number;
-
hexagon.gotoAndStop(1);
-
}
-
}
-
-
addEventListener("enterFrame", enterFramer)
-
-
function enterFramer(event:Event):void {
-
// Get replaces the lastFrame's tile with a blank one
-
MovieClip(hexagon_container.getChildAt(hexagon_hover)).gotoAndStop(1);
-
// Declare vars (enterFrame)
-
var sector_x:Number = Math.floor(mouseX/sector_width);
-
var sector_y:Number = Math.floor(mouseY/sector_height);
-
var delta_sector_x:Number = mouseX%sector_width;
-
var delta_sector_y:Number = mouseY%sector_height;
-
var real_x:Number;
-
var real_y:Number;
-
switch(sector_y%2){
-
case 1:
-
if(delta_sector_x>=hexagon_width/2){
-
if(delta_sector_y<(hexagon_height/2-delta_sector_x*gradient)){
-
real_x = sector_x;
-
real_y = sector_y-1;
-
}
-
else{
-
real_x = sector_x;
-
real_y = sector_y;
-
}
-
}
-
else{
-
if(delta_sector_y<delta_sector_x*gradient){
-
real_x = sector_x;
-
real_y = sector_y-1;
-
}
-
else{
-
real_x = sector_x-1;
-
real_y = sector_y;
-
}
-
}
-
break;
-
case 0:
-
real_x = sector_x;
-
real_y = sector_y;
-
if(delta_sector_y<((hexagon_height/4)-delta_sector_x*gradient)){
-
real_x = sector_x-1;
-
real_y = sector_y-1;
-
}
-
if(delta_sector_y<((-hexagon_height/4)+delta_sector_x*gradient)){
-
real_x = sector_x;
-
real_y = sector_y-1;
-
}
-
break;
-
}
-
if((real_x>=0)&&(real_x<grid_x_size)&&(real_y>=0)&&(real_y<grid_y_size)){
-
hexagon_hover = real_x+real_y*grid_x_size;
-
// Re-adds new hovering tile with red
-
MovieClip(hexagon_container.getChildAt(hexagon_hover)).gotoAndStop(2);
-
}
-
};
Download the source codes and experiment
They can be easily customized to meet the unique requirements of your project.
9 Responses to “Finding adjacent cells in an hex map”
Leave a Reply
Trackbacks
-
Finding adjacent cells in an hex map - part 2 : Emanuele Feronato - italian geek and PROgrammer on
April 27th, 2008 8:42 pm
[...] by Emanuele Feronato on 04/27/08 in Flash, Game design In the Finding adjacent cells in an hex map post I showed you how to find adjacent cells in horizontal hex [...]
-
MiniGladiator » Blog Archive » Javascript Gladiators on
September 12th, 2008 7:15 pm
[...] around while browsing the site.  Just today I found some Actionscript code by Emanuele Feronato (http://www.emanueleferonato.com/2008/04/23/finding-adjacent-cells-in-an-hex-map/) that looks pretty sweet. [...]
-
Halloween Couples : Emanuele Feronato on
November 2nd, 2008 10:17 pm
[...] basics of hexagonal environments are explained in Understanding hexagonal tiles, Finding adjacent cells in an hex map – part 1 and [...]
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 out of 5)



Great, but what do you plan to do with it onc its done ?
Yeah…
It seems…sorta…pointless…
as much as i think eaglevisions comments are pointless, i think he has a point. I dont see where ur going with this. Honeycomb??? was it a request?? but still works fine for whoever would need it. Thanks.
edit: what i did was make those hexagon’s cubes and it look like steps….. it looks sorta cool so try it !!!!!!!
with this code you can find any tile relative to the one your mouse is on, that could be great for puzzle games!
styxtwo is right…