Grid Class
Ever got sick of creating 2 dimensional arrays? Wanted a class to do it for you?
Try this class I whacked together in a flurry of inspiration (I’m kidding myself - give me a break).
dynamic class Grid extends Array{
private var vw:uint
private var vh:uint
public function Grid(vx:uint, vy:uint)
{
vw = vx;
vh = vy;
for (var i:int=0; i< vx; i++)
{
this[i] = new Array(vy);
}
}
public function get width():uint
{
return vw;
}
public function get height():uint
{
return vh;
}
}
What this class does at the moment is relatively simple.
You simply declare a variable as a new Grid like so
var myGrid:Grid = new Grid(10,10);
That will create a 2 dimensional array 10×10. You can then reference it as you’d do a normal 2 dimensional array:
trace(myGrid[4][1]);
And that’s the end of my trivial actionscript pursuits. Enjoy!






February 20th, 2007 at 11:15 am
Really kewl! Thanks!
April 26th, 2007 at 8:15 am
Hey, I wonder how the “dynamic” modifier work here in your class? What is the use of it in AS3. Any tips?
April 26th, 2007 at 5:17 pm
Hi there. Thanks for dropping by!
Well, if you look at this link
http://livedocs.adobe.com/flex/2/langref/Array.html
It states that “You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array.”
Hope that clears your question.