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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

3 thoughts on “Grid Class

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>