The last three articles have been about utility functions for objects, classes, and display objects. This is the finale in the series and contains some leftover utility functions.

First off is a truly random utility method useful in many places for debugging: getStackTrace. This works by throwing an error, catching it, and getting the stack trace out of the error. I find that I call this while printing debug logs so that I can see how the flow of execution got to a certain point, such as how a function was called. This may be less useful if you use a real debugger instead of printing debug logs, but many people rarely use a debugger and rely on debug logging. So, here is getStackTrace:

/**
*   Get a stack trace
*   @return A stack trace
*   @author Jackson Dunstan
*/
public static function getStackTrace(): String
{
	try
	{
		throw new Error();
	}
	catch (err:Error)
	{
		return err.getStackTrace();
	}
	// It's impossible to reach this
	return null;
}

Next up is a set of three functions for accessing a MovieClip‘s library. In general, it accesses an ApplicationDomain, but this is usually in the form of a library. You might want to, as I have, add them to a MyMovieClip base class in a framework. IF you don’t, it’s probably a good idea to add a MovieClip parameter to them. So, here are getClassFromLibrary and two functions that use it: getClipFromLibrary and getBitmapDataFromLibrary.

/**
*   Get a class from the clip's library
*   @param className Name of the class to get
*   @return The class with the given name or null if it cannot be found
*   @author Jackson Dunstan
*/
public function getClassFromLibrary(className:String): Class
{
	try
	{
		return Class(this.loaderInfo.applicationDomain.getDefinition(className));
	}
	catch (refErr:ReferenceError)
	{
		return null;
	}
	catch (typeErr:TypeError)
	{
		return null;
	}
 
	return null;
}
 
/**
*   Get an instance of a clip from the clip's library
*   @param className Name of the clip's class (aka. linkage ID)
*   @return An instance of the clip with the given name or null if the
*           class cannot be found or the clip cannot be instantiated
*   @author Jackson Dunstan
*/
public function getClipFromLibrary(className:String): MovieClip
{
	var clazz:Class = getClassFromLibrary(className);
	if (!clazz)
	{
		return null;
	}
	try
	{
		return new clazz();
	}
	catch (err:ArgumentError)
	{
		return null;
	}
	return null;
}
 
/**
*   Get an instance of a BitmapData from the clip's library
*   @param className Name of the BitmapData's class (aka. linkage ID)
*   @return An instance of the BitmapData with the given name or null if
*           the class cannot be found or the BitmapData cannot be
*           instantiated
*   @author Jackson Dunstan
*/
public function getBitmapDataFromLibrary(className:String): BitmapData
{
	var clazz:Class = getClassFromLibrary(className);
	if (!clazz)
	{
		return null;
	}
	try
	{
		// Need to pass a width and height, but they are ignored
		return new clazz(0, 0);
	}
	catch (err:ArgumentError)
	{
		return null;
	}
	return null;
}

Finally, not a function but a class. This was one of the first classes I wrote in AS3 and greatly helped me transition my handmade event system into AS3’s built-in event system. This is a very simple Event derivative that simply holds generic data, hence the name GenericDataEvent.

package com.disney.base
{
	import flash.events.*;
 
	/**
	*   An event with generic data
	*   @author Jackson Dunstan
	*/
	public class GenericDataEvent extends BaseEvent
	{
		/** Generic data the event has */
		public var data:*;
 
		/**
		*   Make the event
		*   @param type Type of the event
		*   @param data Generic data
		*/
		public function GenericDataEvent(type:String, data:*)
		{
			super(type);
			this.data = data;
		}
	}
}

Well, we’ve come to the end of the series on utility functions. I’m sure I’ll come up with plenty more in the future that I can share with you all and I will likely post them. Until then, feel free to post any of your miscellaneous utility functions below in the comments. There were some pretty nice ones last time, so keep it up!