return undefined;


ArrayCollection for AS2

Posted in AS2, Flash by Ben Clinkinbeard on the June 8th, 2007

Did you know that AS2 includes rudimentary implementations of collections and iterators? I didn't until a few months back, but they do come in handy from time to time. An added bonus is that they make me less sad about developing in AS2 and missing out on all that AS3 goodness when the project requires FP8 compatibility.

Anyhow, the default implementation still leaves a bit to be desired, especially for those of us who spend the majority of our time coding AS3. The built-in class not only bears the unfortunate name CollectionImpl (which implements the Collection interface, both of which reside in the mx.utils package), but it also lacks some methods I've grown to appreciate in AS3's ArrayCollection class. Those methods are as follows:

  • length (getter)
  • addItemAt()
  • contains()
  • getItemIndex()
  • removeAll()
  • removeItemAt()

The following class provides those methods. I know, nothing fancy or impressive, but useful (in my opinion) nonetheless. CollectionImpl isn't all bad though, it does provide us with most of the basic collection-related functionality you would expect, including the removeItem() method that was left off of AS3's ArrayCollection class. As such, this class inherits from CollectionImpl and attempts to fill in the blanks.

Actionscript:
  1. import mx.utils.Collection;
  2. import mx.utils.CollectionImpl;
  3.  
  4. class com.returnundefined.utils.ArrayCollection extends CollectionImpl implements Collection
  5. {
  6.     private var _items:Array;
  7.    
  8.     public function ArrayCollection(arr:Array)
  9.     {
  10.         super();
  11.         _items = (arr == null) ? new Array() : arr;
  12.     }
  13.    
  14.     public function get length():Number
  15.     {
  16.         return _items.length;
  17.     }
  18.    
  19.     public function addItemAt(item:Object, index:Number):Boolean
  20.     {
  21.         var result:Boolean = false;
  22.        
  23.         if(item != null)
  24.         {
  25.             _items.splice(index, 0, item);
  26.             result = true;
  27.         }
  28.        
  29.         return result;
  30.     }
  31.    
  32.     public function contains(item:Object):Boolean
  33.     {
  34.         return getItemIndex> -1;
  35.     }
  36.    
  37.     public function getItemIndex(item:Object):Number
  38.     {
  39.         var index:Number = -1;
  40.         var len:Number = length;
  41.         for(var i:Number = 0; i <len; i++)
  42.         {
  43.             if (_items[i] == item)
  44.             {
  45.                 index = i;
  46.                 break;
  47.             }
  48.         }
  49.         return index;
  50.     }
  51.    
  52.     public function removeAll():Void
  53.     {
  54.         _items = new Array();
  55.     }
  56.    
  57.     public function removeItemAt(index:Number):Boolean
  58.     {
  59.         return removeItem(getItemAt(index));
  60.     }
  61. }

Feel free to alter the package path here and insert this class into your library wherever you want.

4 Responses to 'ArrayCollection for AS2'

Subscribe to comments with RSS or TrackBack to 'ArrayCollection for AS2'.

  1. Anonymous said,

    on October 1st, 2007 at 11:36 pm

    AS3 doesn't have ArrayCollection, the Flex framework does.

  2. Ben said,

    on October 1st, 2007 at 11:52 pm

    And the Flex framework is written in AS3, no?

  3. Anonymous said,

    on October 2nd, 2007 at 12:47 am

    AS3 is necessary to have ArrayCollection, but not sufficient.

  4. Ben said,

    on October 2nd, 2007 at 1:35 am

    Um, what?

Leave a Reply