View comments | RSS feed

UIObject.createClassObject()

Availability

Flash Player 6 (6.0.79.0).

Edition

Flash MX 2004.

Usage

componentInstance.createClassObject(className, instanceName, depth, initObject)

Parameters

className An object indicating the class of the new instance.

instanceName A string indicating the instance name of the new instance.

depth A number indicating the depth of the new instance.

initObject An object containing initialization properties for the new instance.

Returns

A UIObject object that is an instance of the specified class.

Description

Method; creates an instance of a component at runtime. Use the import statement and specify the class package name before you call this method. In addition, the component must be in the FLA file's library.

Example

The following code imports the assets of the Button component and then makes a subobject of the Button component:

import mx.controls.Button;
createClassObject(Button,"button2",5,{label:"Test Button"});

The following example creates a CheckBox object:

import mx.controls.CheckBox;
form.createClassObject(CheckBox, "cb", 0, {label:"Check this"});

You can also use the following syntax to specify the class package name:

createClassObject(mx.controls.Button, "button2", 5, {label:"Test Button"});

Flash CS3


Comments


Dennis O'Brien said on Feb 25, 2009 at 7:42 PM :
If you try the examples here from a class that extends MovieClip, you will need to either make your class dynamic, or make it extend mx.core.UIObject.

Just burnt an hour on this, so I thought I'd share.

To reproduce:
1) Make a new AS2 fla. Save it somewhere.

2) Drag a Button symbol from the Components panel to your library.

3) Create a new movieclip in the library.
call it mcScreenWithButtonComponent
export for AS
id: mcScreenWithButtonComponent
Class: MCScreenWithButtonComponent

4) Create the class file MCScreenWithButtonComponent.as in the same folder as the fla.
// begin
import mx.controls.Button;
import mx.core.UIObject;

class MCScreenWithButtonComponent extends MovieClip
{
public var btn:MovieClip;

function MCScreenWithButtonComponent()
{
createClassObject(mx.controls.Button, "btn", this.getNextHighestDepth(), {label:"Test Button"});
}
}
// end

5) On the first frame of the fla, instantiate the class.
_root.attachMovie("mcScreenWithButtonComponent", "mcScreen", this.getNextHighestDepth());

6) Compile. The Compiler Errors dialog display: "There is no method with the name 'createClassObject'."

7) Make your class dynamic:
dynamic class MCScreenWithButtonComponent extends MovieClip
Or, change the class to extend mx.core.UIObject instead of MovieClip.
class MCScreenWithButtonComponent extends mx.core.UIObject
No compile problem, and the button appears.


I've been bitten by not including the dynamic keyword many times, but this one was not so obvious.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00003653.html