PDA

View Full Version : [AS3] Adding a movieclip to a Hextiled grid

ShamRockIII
04-23-2010, 05:40 AM
Ok, I'm making a hextile game and I want to dynamically add movieclips to a grid built with an array. I have two movieclips in the library, HexField which lays out the grid and HexCard which is the unit I want to add to the stage.

Here's the code I have so far...

package
{
import flash.display.MovieClip;
public class Field extends MovieClip
{
var hexWidth:Number = 85.0;
var hexHeight:Number = 98.0;
var gridX:int = 9;
var gridY:int = 9;

public function Field():void
{
var fieldArray:Array = new Array();
var hexCard:HexCard = new HexCard;

function layField():void
{
for (var y_pos:int =0; y_pos<gridY; y_pos++)
{
//fieldArray[y_pos] = new Array();
for (var x_pos:int =0; x_pos<gridX; x_pos++)
{
var hexagonXPos:Number = hexWidth*x_pos+(y_pos%2)*hexWidth/2;
var hexagonYPos:Number = (hexHeight * y_pos * 0.75);
var hexagon:HexField = new HexField();
hexagon.x = hexagonXPos;
hexagon.y = hexagonYPos;
//fieldArray[x_pos][y_pos] = hexagon;
}

}
}
layField();

}// end of Field function

}// end of class

}// end of package

I'm pretty sure it's the commented out code that's giving me trouble but I can't figure out what to do with it. The swf only displays 2 HexField tiles and the output gives:
TypeError: Error #1010: A term is undefined and has no properties.
at MethodInfo-1()
at Field()

Please any help would be appreciated.

Sham

svenjoypro
04-23-2010, 05:54 AM
I think this will solve your problem:

package
{
import flash.display.MovieClip;
public class Field extends MovieClip
{
var hexWidth:Number = 85.0;
var hexHeight:Number = 98.0;
var gridX:int = 9;
var gridY:int = 9;

public function Field():void
{
var fieldArray:Array = new Array();
var hexCard:HexCard = new HexCard;

function layField():void
{
for (var y_pos:int =0; y_pos<gridY; y_pos++)
{
fieldArray.push(new Array());
for (var x_pos:int =0; x_pos<gridX; x_pos++)
{
var hexagonXPos:Number = hexWidth*x_pos+(y_pos%2)*hexWidth/2;
var hexagonYPos:Number = (hexHeight * y_pos * 0.75);
var hexagon:HexField = new HexField();
hexagon.x = hexagonXPos;
hexagon.y = hexagonYPos;
fieldArray[y_pos].push(hexagon);
}

}
}
layField();

}// end of Field function

}// end of class

}// end of package

ShamRockIII
04-23-2010, 06:01 AM
Absolute magic! That works thank you.

Thanks Svenjoypro

pepeu
11-16-2010, 02:02 PM
Sorry for bumping, but there is a great tutorial over here: tonypa.pri.ee/tbw/tut25.html

Btw, how is the game progressing? A sneak preview perhaps?

Cheers!