FLEX2 Discovery Tour: Flex2 New Live Component instance method

Source: Internet
Author: User
Tags addchild instance method
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 ...:)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.