Dynamic Assigment of AS2.0 Classes

Riddle~

Assigning ActionScript classes to MovieClip library assets has changed the way we develop user interfaces. Components add behavior directly to a display element, combining function with the design. The ability to extend the MovieClip class simplifies relationships and allows for powerful display objects that can be reused. But it can only be achieved through the linkage of a MovieClip in the library. TextFields and Buttons (Flash native Buttons, not to be confused with other component Buttons) are exempt, as well as a very important MovieClip: the root.

Answer:
The static Prototype class is a simple collection of methods that solve these limitaions. The most important of these is Prototype.makeInstanceof. The makeInstanceof method simply makes an object an instance of the specified class and calls the constructor (the same way a class is constructed behind the scenes). It is best used for display elements that can’t be created through the new operator: MovieClip, Button and TextField. Through the makeInstanceof method the _root can also be turned into a custom object, perhaps to become your main Application class.

The other major function of Prototype is inherit. Rather than refacing an object to become a different type, it copies the properties and methods as an addition to its current functionality. A prime example of this behavior is the AsBroadcaster (Flash event dispatching). By calling invoking AsBroadcaster.initialize(obj) on an object it inherits a handful of new members: the addListener and broadcastMessage methods are two of them. This process does not tamper with the existing functionality of an object and so allows any number of behavoirs to be added to an instance at runtime.

Note, for developers who feel the methods of the Prototype class are poor OOP: because of some basic OO limitations of Flash (ActionScript fully implemented as a prototype language) you must make several allowances. One, makeInstanceof compensates for the lack of support in extending several of the Flash core Classes. These of course being TextField, Button and in the case of _root, MovieClip. Second, inherit allows, more or less, a functional interface. By using Prototype’s inherit properly, functionality can be coded once and reused as a modual (Decorator) through all classes that implement the class. Changes to behavior and bug fixes are, appropriately, isolated to that single class.

Make sure that as you use these techniques you maintain an organized and documented code base. When applying new functionality to a class or object you can avoid conflicts by taking care of all additions immediately following object creation. You should also declare and document additional members as if they were an original part of the affected class.

example:


import xt.managers.DepthManager;
import xt.events.EventDispatcher
class com.codext.core.UIComponent extends MovieClip
{
	// :: Public Properties :: //
	// DepthManagement
	public var depth:Number;
	public var getNextDepth:Function;
	public var bringToFront:Function;
	public var bringForward:Function;
	public var sendBackward:Function;
	public var sendToBack:Function;
	// Events
	public var hasEventListener:Function;
	public var addEventListener:Function;
	public var dispatchEvent:Function;
	public var removeEventListener:Function;
	/**
	 * Constructor description...
	 */
	private function UIComponent()
	{
		// first item upon object instantiation
		Prototype.makeInstanceof(this, EventDispatcher);
		// DepthManager often assigns functionality to
		// the MovieClip.prototype
	}
}

This class is available here and will also be included as a core class of the coming Flight component framework.
Download the Prototype class

1 Comments

  1. Posted on April 3, 2007 at 6:21 pm by Curtis J. Morley

    Great example. It is things like this that make you a Flash Forward speaker. Thanks this is very cool. Looking forward to the component set.

The comments are closed.