Semi-Typed Array in Flash Player 9

11 11 2008

A few days ago I stumbled over the ActionScript 3 Vector class. This class is like an array but it has one (beside others) big advantage: compile and runtime type-checking!

So I headed out to see how it works…and I stuck. My problem was that the Vector class is only available for Flash Player 10+ but I’m targeting Flash Player 9. So I decided to make something similar like this (and add some convinient methods):

ExtendedArray and ExtendedKeyArray

The ExtendedArray

Constructor

public function ExtendedArray(typeClass:Class=null)

The constructor gets the desired type and saves it internally. Whenever an item is added, ExtendedArray checks wheater is the right type (or subclass) or not. If that check fails an error it thrown so you know if there would have been something added that wouldn’t belong here.

This constructor is different and not compatible with the original constructor of Array.

Methods

As ExtendedArray subclasses Array all the methods of array are still there.

The following methods are added:

public function contains(itemj:*):Boolean

Checks wheater or not an item is in the array.

public function clear():void

Clears the array.

public function move(from:int=-1,to:int=-1):void

Moves an item from one place to another.

public function getIndex(item:*):int

Searches an item and returns its index.

public function remove(item:*):Boolean

Searches for an item and deletes it.

public function removeAt(i:uint):*

Deletes an item on a specific index position.

public function pushRange(…array):void

This method is dedicated to pushing the items of an array (not the array itself). It one of the arrayitems is of the wrong type an error is thrown and no item will be added. Arrays can’t be added with push (except the desired type is an array ;) ) they must be added with pushRange.

The ExtendedKeyArray

ExtendedKeyArray is a subclass of ExtendedArray, so it behaves similar to it. The difference are the keys that items have in this Array. You can access the data not only through the index but also through the keys. Each key must be unique – otherwise an error is thrown (again ;) ).

So, let’s take a look at the api.

Constructor

public function ExtendedKeyArray(keyClass:Class=null,typeClass:Class=null)

The constructor now takes two arguments:

  • the type of the key
  • the type of the data

Methods

override AS3 function push(…args):uint

The push method now works a little different. The first argument must be the key object and the last one the data item. Actually you must think of push like it had this deklaration:

override AS3 function push(keyObject:*, item:*):uint

override public function pushRange(…args):void

The pushRange is likewise different. The first argument here is an array of key-objects. The second argument is also an array but of item-objects. If these two arrays aren’t exactly the same length or have an wrong key-/item-type, an error is thrown.

public function getByKey(key:*):*

This function gets your added item by passing the key-object.

public function setByKey(key:*,value:*):void

This function associates your already existing key with a new item – the former item will be lost.

public function getKeyIndex(key:*):int

By using this function you can evaluate the indices of your added items through their keys.

public function get keys():Array

Gets you a copy of the current keys array.

Events

If an item is added, moved or removed in ExtendedArray or ExtendedKeyArray an respective event will be fired. The event ExtendedArrayEvent will be dispatched.

public static const ITEM_ADDED:String = “itemAdded”;

An item was added to the array.

public static const ITEM_REMOVED:String = “itemRemoved”;

An item was removed from the array.

public static const ITEM_MOVED:String = “itemMoved”;

An item changed its position in the array.

The ExtendedArrayEvent has three variables:

  • public var item:*= null;
  • public var oldIndex:int = -1;
  • public var newIndex:int = -1;

Every event fills these variables.

Download

So, having written a little documentation, here’s the project for download:

http://www.megaupload.com/?d=8Z6G57TH

I hope you like it and find it useful!

Warappa

Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.