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. You need to use the import statement and specify the class package name before calling 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 specify the class package name using the following syntax:

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

Comments


erank said on Dec 10, 2004 at 12:55 PM :
I have been trying to add a TabBar component on the fly with:

var tabBar : TabBar = createClassObject( mx.controls.TabBar, "tabBar", getNextHighestDepth() );

I am getting compile errors that say that it is finding mx.core.UIObject where mx.controls.TabBar is required. I think what is happening is that in the class file for the mx.controls.TabBar the classname variable is pointing to UIObject not TabBar.
jepo said on Dec 14, 2004 at 4:12 PM :
Remember the import statement: import mx.controls.TabBar;

Try the following code:

import mx.controls.TabBar;
var myBar:TabBar = createClassObject(TabBar,"myTabBar", 999);
myTabBar.addItem({label:"One"});
myTabBar.addItem({label:"Two"});

(PS, not mandatory becuase you changed the case, but I'd also recommend not using the class name as an instance name).

HTH,
Jen.
No screen name said on Mar 6, 2005 at 7:54 AM :
I use Flash MS Pro. I have shortly a strange problem. UIObejct.createClassObject() does not work at all. I copy and paste examples, but no buttons nor checkboxes were created by the ActionScript.
I also tryed to create a TabBar like Jepo said in example above. In this case I have got two errors:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: The class 'mx.controls.TabBar' could not be loaded.
import mx.controls.TabBar;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: The class 'TabBar' could not be loaded.
var myBar:TabBar = createClassObject(TabBar,"myTabBar", 999);

Total ActionScript Errors: 2 Reported Errors: 2

What is going on? Is my version of Flash somehow get corrupt? About a month ago I was able to generate buttons and lists with my ActionScript. Thos fla's still work, also if I make changes, but I can't create a new fla with creating the buttons with ActionScript.
mmtechwriter said on Mar 8, 2005 at 9:29 AM :
No screen name,

Whatever component you're working with (Button, TabBar, etc.), make sure you have a symbol for that component in the Library (add one to the Stage, then delete it).
Also, keep in mind, the TabBar componnet is only available as part of a Developer Resource Kit, and does not come with Flash MX2004 Pro, so you may not have it in your list of available components.
No screen name said on Mar 16, 2005 at 5:28 AM :
If createClassObject returns null (and won't create objects), make sure that the symbol in your library that you are trying to instantiate is set to export on the first frame (or is somehow already available at the point of instantiation).
jepo said on Jun 7, 2005 at 12:37 PM :
Thank you for your comments. This site is for documentation feedback only. Some comments with general questions about how to use components, bug reports, or feature requests for the Flash product, have been removed. Please use the Flash webforums for questions about how to use components: http://webforums.macromedia.com/flash. Please use this form for feature requests or suspected bugs: www.macromedia.com/support/email/wishform/.
No screen name said on Aug 3, 2005 at 8:08 PM :
Hi, I have met the same problem. Here is my code:

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


Here is the output:

Test Button
undefined
Test New Button
No screen name said on Aug 12, 2005 at 7:14 PM :
No screen name: The problem you're seeing is actually one of depths.

In both your examples you load your buttons into depth "5". As a result the second button replaces the first. Try loading one button into 5, and the other into 6.
RabidLemming said on Dec 16, 2005 at 6:35 AM :
In flash 8 then old method of

import mx.controls.RadioButton;
this.createClassObject(RadioButton, "myTabBar", 999, {label:"a", groupName:"skill", fontFamily:"Verdana", fontSize:"12"});

No longer works as you will find you get a lot of erros. It seams that flash 8 doesn’t support this any more only flash mx 2004 dose so you can use flash mx 2004 and it will work. Or you can follow this work around method here : http://www.flashguru.co.uk/dynamically-attaching-components/

Personally I think macromedia made a mistake buy not allowing this to work in flash 8 but never mind I guess
No screen name said on Jan 11, 2006 at 3:06 PM :
createClassObject(Button,"button2",5,{label:"Test Button"});
trace(button2.label);
createClassObject(Button,"button3",5,{label:"Test New Button"});

you create on the same depth...
jdehaan said on Jan 18, 2006 at 1:42 PM :
Please note that Macromedia TabBar component was only available in an earlier DRK release, which is not currently available for purchase or download (at the time of writing).
jdehaan said on Jan 18, 2006 at 1:59 PM :
Also note that TabBar is part of Flex 1.5 and Flex 2. Flex 2 is available as a preview pre-release at http://labs.adobe.com.

 

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

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00002932.html