Create
In Flex1.5, we can use the Createchild () method if we want to create a component instance in real time.
For example, suppose the following code is in Myapp.mxml:
<mx:Script>
Import Mx.controls.Button;
var Stopbutton:button;
function Someeventhandler (): Void
{
Stopbutton = button (form1.createchild (button, undefined, {label: "stop!"});
}
</mx:Script>
The only way to do this is to do the following four things at the same time:
Creates an instance of a particular class (Button) (Stopbutton).
Use the Initobj ({label: "stop!"} "Sets the property (label) for the new instance.
Attaches the newly created instance to a parent container (Form1).
To separate the other sibling instances, set the _name property of the instance to a unique string value similar to __button17.
In fact, internally, Createchild () calls the Attachmovie () method of the MovieClip class to complete the creation of the instance.
In Flex2, a method like Createchild () will no longer have to be the appropriate method, because in Flash Player 8.5, you can create a visual object like other objects by using the new operation, and when a visual object instance is created, It is not a parent, we can add it to the parent container, we can then remove it and add it to the other parent container (yes, Flash finally supports re-parenting), or we can move it over and let it be reclaimed by the garbage collector.
The new dynamic real-time instance creation method is as follows:
Import Mx.controls.Button;
var Stopbutton:button;
function Someeventhandler (): Void
{
Stopbutton = new Button ();
Stopbutton.label = "stop!";
Stopbutton.setstyle ("Color", 0xff0000);
Form1.addchild (Stopbutton);
}
The above uses more lines of code, but he is more clear and easy to understand:
Creates a new instance by using the new operator.
Sets the properties and styles of the new instance using the normal assignment statement and the SetStyle () method.
An explicit call to the Addchild () method adds the new instance to the parent component.
Note: Other APIs, such as: Destroychild (), Destroychildat (), and Destroyallchildren () methods and removed, replace them: RemoveChild (), Removechildat () and Removeallchildren () method.
Finally, keep in mind the cycle of component creation in the following Flex2:
Create (new)-Add (Add)-Remove (remove)-(add-Remove-...)-be garbage collected
Original address:creating Component instances at Runtime
In addition, today we know that the original Flash Player 8.5 Development code Name: Zaphod
Ps:firefox View selected part of the source code is really convenient, ie some learn oh ...:)