Undolibrary MatrixArray: an extension of class Array

Monday, 15 September, 2008

MatrixArray is an extension of class Array of Actionscript 3.0, with the aim to simplify the handling of Array used as a linear array n x m. This class is part of the library Undolibrary (in the package undolibrary.utils.MatrixArray that you can freely from the download Google code through any client SVN (Subversion) at:
svn checkout http://undolibrary.googlecode.com/svn/trunk/
Or you can just download the file MatrixArray.as. An example of the use of linear arrays has been given in Actionscript 3.0 for beginners: lesson # 2 where we saw a simple use in the implementation of the classic game Tic Tac Toe. A matrix of this type is comparable to a rectangular area w x h just like a chessboard. With this class you can treat an array such as a grid with its x and y coordinates. The MatrixArray I used, for example, development of the whole logic of the editor Emotions Icons for Skype: Skypemote.

Methods

The class MatrixArray therefore extends the normal class Array adding a number of useful ways to manage and manipulate m x n matrix. To create a matrix n x m is enough to use:

Actionscript:
  1. / / Importanzione class - if you downloaded the entire library Undolibrary
  2. / / Alternatively you can just download the file and use it MatrixArray.as alone
  3. . MatrixArray ; import undolibrary. utils. MatrixArray;
  4. / /
  5. MatrixArray ( 10 , 10 ) ; var but: MatrixArray = new MatrixArray (10, 10);

Created the 10x10 matrix, as in the previous example, we can manipulate it with useful methods that make our work very rapidly and easily. In fact, the 10x10 matrix is nothing more than a simple linear array of 100 items!

clip ()

Actionscript:
  1. / **
  2. * Restitusice a rectangular portion of the matrix in the form MatrixArray
  3. *
  4. * @ Param (UINT) left coordinates x cutting
  5. * @ Param (UINT) sy y coordinates of the cut
  6. * @ Param (UINT) cw horizontal size of the cut
  7. * @ Param (UINT) ch vertical size of the cut
  8. * @ Result (MatrixArray) A new matrix x cw ch
  9. *
  10. * /
  11. :MatrixArray clip (left: UINT, sy: UINT, cw: UINT, ch: UINT): MatrixArray
  12. / / Example
  13. / / Create a new MatrixArray coordinates 2,2 and large and high-5
  14. ( 2 , 2 , 5 , 5 ) ; var NMa: MatrixArray = ma. clip (2, 2, 5, 5);

fill ()

Actionscript:
  1. / **
  2. * Fill the array with a value
  3. *
  4. * @ Param (any) value to be included in the matrix
  5. * /
  6. ; fill (v *);

paintChar ()

Actionscript:
  1. / **
  2. * Fill the array with one (only) a sample from a string
  3. * Length equal to the size of the matrix itself
  4. *
  5. * @ Param (string) v string with the number of characters to map
  6. * @ Result (boolean) ok true, false error: string length wrong
  7. *
  8. * /
  9. ) paintChar (v: string)
  10. / / Example
  11. MatrixArray ( 10 , 5 ) ; var but: MatrixArray = new MatrixArray (10, 5);
  12. ma. paintChar (
  13. '**********' +
  14. '**##**##**' +
  15. '**********' +
  16. '**#####**' +
  17. '**********'
  18. );

flipH (), flipV ()

Actionscript:
  1. / **
  2. * Performs a reflection orizziontale (flipH) or
  3. * Vertical (flipV) directly on the matrix
  4. * /
  5. flipH (): void
  6. flipV (): void

move ()

Actionscript:
  1. / **
  2. * Move the contents of the matrix (running a shift) in the four
  3. * Direction of a predetermined offeset
  4. * /
  5. , oy: int ) : void move (ox: int, oy: int): void
  6. / /
  7. MatrixArray ( 10 , 5 ) ; var but: MatrixArray = new MatrixArray (10, 5);
  8. ma. paintChar (
  9. '**********' +
  10. '**##**##**' +
  11. '**********' +
  12. '**#####**' +
  13. '**********'
  14. );
  15. 2 , 4 ) ; ma. move (2, 4);
  16. / / Output
  17. **** ##**##
  18. **********
  19. **** #####
  20. **********
  21. **********

peek ()

Actionscript:
  1. / **
  2. * Returns the contents of the grid coordinates x, y
  3. * /
  4. :* peek (x: UINT, y: UINT): *

poke ()

Actionscript:
  1. / **
  2. * Sets the contents of the grid coordinates x, y
  3. * /
  4. : void poke (x: UINT, y: UINT): void

getString ()

Actionscript:
  1. / **
  2. * Used for debugging often returns the matrix as a string nxm
  3. * /
  4. getString (): String

Related Post

Leave a comment

TAG XHTML <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <a <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Use <pre> to enclose code