Array "class" for FlashLite

I've been playing with FlashLite as of late and it's actually not as difficult as I first thought (I first gave it a look and then gave up a few month's ago.) It feels naughty to be placing my code on the timeline, in movie clips, etc. again but there are many things you can do to make FlashLite apps and games easier to maintain.

Today, I had some spare time on the train to London and created a little Array "class" (well, movie clip). It implements the most popular methods as well as some properties/behaviors that have been added as "methods" (push, pop, lookUp, assign, length, etc.)

Here's an example of usage (think of the Array movie clip as a static class that contains all the arrays in your app):

ActionScript:
  1. tellTarget ( "Array" )
  2. {
  3. // Add first item, the string "Hello"
  4. name = "myArray";
  5. item = "Hello";
  6. call ("push");
  7. }

It even comes with a suite of unit tests... yes, you read it here first -- unit tests for FlashLite :) Wanna see an example of a unit test in Flash 4/FlashLite? Here goes:

ActionScript:
  1. tellTarget ( "Array" )
  2. {
  3. index = 1;
  4. call ( "lookUp" );
  5. }
  6.  
  7. // Assert that the value at index 1 is "There"
  8. if ( eval ( "/Array:value" ) ne "There" )
  9. {
  10. trace ( "Assert failed on testLookUp: Value should be \"There\"" );
  11. failed++;
  12. }
  13. else passed++;

(OOPsters may keep their chuckles to themselves, thank-you-very-much!) ;)

Download the FLA and take a look (flashlite_array.zip; 8kb). [MIT license, go crazy with it if you like!]

One caveat: It's considerably slower than rolling your own using eval on the same timeline/object. I'm actually not sure whether I'll end up using it on the little game I'm making and I'd appreciate your comments/thoughts.

Comments