AdvArray for Actionscript 3

Published on Oct. 01, 2008
by Kyle

I wrote a brief class extending Actionscript 3’s Array class. Each time that I have found myself repeating common array tasks I have tried to add it. The AdvArray Documentation contains a list of the methods I have added. The ones I find the most helpful are probably clone() and rotate().


package com.hapticdata {

	import flash.utils.ByteArray;

	/**
	 * Adds extra methods to arrays for easier manipulation
	 * @class AdvArray
	 * @author Kyle Phillips - http://www.haptic-data.com
	 * @created Jun 29, 2008
	 */
	dynamic public class AdvArray extends Array {
		public function AdvArray(...args) {
			//this = this as Array;
			for each(var i:* in args)
			{
				super.push(i);
			}
			//super(...args);
		}

		 /**
		  * add an array to the end of the existing AdvArray
		  * @param a (Array) the array to append
		  * @return the new length of the array
		  */
		 public function append(a:Array):uint
		 {
		 	for(var i:uint=0;i < a.length;i++)
		 	{
		 		super.push(a[i]);
		 	}
		 	return super.length;
		 }
		 /**
		  * add an array at a given index of the existing AdvArray, defaults to the beginning of the array
		  * @param a (Array) the array to append
		  * @param index (uint) the index of the array to start placing the new array
		  * @return the new length of the array
		  */
		 public function appendAt(a:Array,index:uint=0):uint
		 {
		 	append(a);
		 	rotate(((a.length)+index)*-1);
		 	return super.length;
		 }
		/**
		 * allows you to search the array for a specific value, similar to MovieClip.contains()
		 * @param i (*) the value you are searching the array for
		 * @return returns Boolean
		 * @example
		 * if(myArray.contains("my string"))var myString:String = myArray.pluck(myArray.indexOf("my string"));
		 */
		public function contains(i:*):Boolean
		{
			var index:int = this.indexOf(i);
			return (index != -1) ? true : false;
		}

		/**
		 * pull an item out of the array by its index and re-sort the array
		 * @param index (int) the index of the array to pluck
		 * @return the item that was removed from the array
		 */
		public function pluck(index:int):* {
			return super.splice(index,1);
		}

		/**
		 * allows you to cycle an array one step forward, placing the first item at the end of the array
		 * @return returns the new AdvArray
		 */
		public function rotateForward():Array {
			var firstItem:* = super.shift();
			super.push(firstItem);
			return this;
		}
		/**
		 * allows you to cycle an array one step backwards, placing the last item at the beginning of the array
		 * @return returns the new AdvArray
		 */
		public function rotateBackwards():Array {
			var lastItem:* = super.pop();
			super.unshift(lastItem);
			return this;
		}
		/**
		 * allows you to specify a positive or negative value to cycle the array, 0 will do nothing
		 * @param inc (int) positive or negative value for direction and amount to rotate the array
		 * @return returns the new AdvArray
		 */
		public function rotate(inc:int=1):Array {
			if(inc > 0)
			{
				for(var j:int=0;j < inc;j++) rotateForward();
			}
			else if(inc<0)
			{
				for(var k:int=0;k > inc;k--)rotateBackwards();
			}
			return this;
		}
		/**
		 * makes a shallow copy of the current array
		 * Flex3 LiveDocs: In a shallow copy, if the original array has elements that are objects, only the
		 * references to the objects are copied rather than the objects themselves. The copy points to the same objects as the original does.
		 * Any changes made to the objects are reflected in both arrays.
		 * @return a shallow copy of the current array
		 */
		public function shallowCopy():Array
		{
			return super.concat();
		}

		/**
		 * makes a deep copy of the current array
		 * Flex3 LiveDocs: In a deep copy, any objects found in the original array are also copied
		 * so the the new array does not point to the same objects as does the original array. 
		 * @return a deep copy of the current array
		 */
		public function clone():Array
		{
    		var myBA:ByteArray = new ByteArray();
    		myBA.writeObject(super);
    		myBA.position = 0;
    		return(myBA.readObject());
		}

	}
}

Download the source:
AdvArray.as

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Responses are currently closed, but you can trackback from your own site.

Phase Shifts

My recent past has been full of transition and I’m very excited to announce the recent changes in my life. Recently… I participated in the Twin Cities Maker Minne-Faire; I stayed up for more than 24 hours straight with a group of talented friends building a website for Child Protection International (new site not up [...]

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Colle + McVoy Responsive Signage

To express our capabilities and make our space speak to what we do, we enhanced our existing plastic logotype with a hidden projector and camera. Mapping our projections to the space, we created sequences of illumination for the lettering and created a play between virtual and physical objects that responds to the activity of people in the space.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Art(ists) on the Verge Fellowship

I am one of five recipients of the Northern Lights organization’s Art(ists) on the Verge Fellowship funded by the Jerome Foundation.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Squawq Analytics – Term metrics for Twitter

The launch of Squawq, a powerful analytics tool for brands and Twitter. Squawq stores information about terms so that a user may monitor its popularity and know what is being said.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Cyclist Mural

As avid bikers at Colle + McVoy we rose to the occasion and created a mural, using rear-projection and top-mounted camera. We decided to show the many styles of cycling and have some fun with it!

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Twaugmented Reality

Our team created an augmented-reality site using fiducial-tracking (the type with the paper square) and I was the lead developer.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Catch-a-Boat was a great website but…

Catch-a-Boat was a great website, and I’m gonna let it have its moment, but…

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter