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!

Share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Facebook
  • Google Bookmarks

Tags: ,

3 Responses to “Grid Class”

  1. Zhet Says:

    Really kewl! Thanks!

  2. awflasher Says:

    Hey, I wonder how the “dynamic” modifier work here in your class? What is the use of it in AS3. Any tips?

  3. Daryl Says:

    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.

Leave a Reply