N-R > Object.registerClass

 

Object.registerClass

Availability

Flash Player 6

Usage

Object.registerClass(symbolID, theClass)

Parameters

symbolID The linkage identifier of the movie clip symbol, or the string identifier for the ActionScript class.

theClass A reference to the constructor function of the ActionScript class, or null to unregister the symbol.

Returns

If the class registration succeeds, a value of true is returned; otherwise, false is returned.

Decription

Method; associates a movie clip symbol with an ActionScript object class. If a symbol doesn't exist, Flash creates an association between a string identifier and an object class.

When an instance of the specified movie clip symbol is placed by the Timeline, it is registered to the class specified by the theClass parameter rather than to class MovieClip.

When an instance of the specified movie clip symbol is created using the attachMovie or duplicateMovieClip methods, it is registered to the class specified by the theClass parameter rather than to class MovieClip.

If theClass is null, Object.registerClass removes any ActionScript class definition associated with the specified movie clip symbol or class identifier. For movie clip symbols, any existing instances of the movie clip remain unchanged, but new instances of the symbol are associated with the default class MovieClip.

If a symbol is already registered to a class, the Object.registerClass method replaces it with the new registration.

When a movie clip instance is placed by the Timeline or created using attachMovie or duplicateMovieClip, ActionScript invokes the constructor for the appropriate class with the keyword this pointing to the object. The constructor function is invoked with no parameters.

If you use the Object.registerClass method to register a movie clip with an ActionScript class other than MovieClip, the movie clip symbol doesn't inherit the mothods, properties, and events of the built-in MovieClip class unless you include the MovieClip class in the prototype chain of the new class. The following code creates a new ActionScript class called theClass that inherits the properties of the MovieClip class:

theClass.prototype = new MovieClip();

Example

This example creates a component for a standard check box UI widget.

First you create a movie clip symbol called Check Box in the library. Then you create a CheckBox class in ActionScript which will represent the check box.

// Define constructor for (and thus define) 
	CheckBox class

function CheckBox() {
...
}

// Set CheckBox prototype chain 
	to inherit from MovieClip

CheckBox.prototype = new MovieClip();

// Define methods for the CheckBox class

CheckBox.prototype.setLabel = function (newLabel) {
	this.label = newLabel;
	...
};
CheckBox.prototype.setValue = function (newValue) {
	this.value = value;
	...
};
CheckBox.prototype.getValue = function () {
	return this.value;
};
CheckBox.prototype.getLabel = function () {
	return this.label;
};

Now you must associate the CheckBox class with the Check Box movie clip symbol. First, you need the ability to identify the Check Box movie clip symbol with ActionScript. To do this, enter an identifier in the Linkage dialog box in the library and select Export for ActionScript.

Next, write ActionScript to associate the CheckBox class with the CheckBox symbol:

Object.registerClass("CheckBox" /*symbolID*/, CheckBox /*theClass*/ );

Usage 1(Timeline placement):You can now place instances of CheckBox on the stage in the authoring tool, and at runtime the instances will automatically receive the ActionScript class CheckBox. If you place two instances, myCheckBox1 and myCheckBox2, you can control them by invoking methods, as in the following:

myCheckBox1.setValue(true);
myCheckBox2.setValue(false);
myCheckBox2.setLabel("new label for #2");

Usage 2 (Dynamic instances): You can use the attachMovie method to create a new instance of the check box on the Stage as the movie plays. Because the CheckBox symbol is registered to the ActionScript class CheckBox, the new dynamic instance will automatically receive that class.

// createCheckBox is a helper function that
// dynamically creates CheckBoxes
function createCheckBox(name, depth) {
	attachMovie("CheckBox", name, depth);
}
createCheckBox("myCheckBox3", 100);
myCheckBox3.setValue(false);
myCheckBox3.setLabel("new label for #3");

See also

MovieClip.attachMovie, MovieClip.duplicateMovieClip