Flash as3 basics: getchildbyname in as3)

Source: Internet
Author: User
In as3, we can use getchildbyname to obtain a component, but note that the returned type is displayobject, so that once our component has some custom methods, it cannot be used.

 

For example, if I have a ball class and ball has a move method, I can write it like this:

VaR B: ball = new ball ();

Addchild (B );

B. Move (); // No problem

Getchildbyname ("B"). Move (); // an error is returned during compilation.

The reason should be that the type returned by getchildbyname is displayobject instead of ball, so the ball method cannot be called.

Write the following statement:

VaR C: ball = getchildbyname ("B") as ball;

C. Move ();

In this way, the ball method can be called after compilation.

 

The following is my summary:

The getchildbyname method is used for dynamic component naming. For example, if you have a component in the scene and name it "cc" in the attribute panel, you can write CC when you operate on it. y = xxx; in dynamic naming, this component is not found in the above example. You can use addchild to place it in the scenario and then name it, in this case, getchildbyname is required to operate the component. The following is an example:

VaR B: ball = new ball ()

Addchild (B)

B. Name = "mc1"

Get childbyname ("mc1"). Y = 200

 

Supplement:

For example, the mc1 component in the scenario uses a string to represent it and uses this string to call its attributes or methods:

VaR cc = "mc1"

Getchildbyname (cc). xxx

In this case, if it is a custom property or method, an error will be reported, because as3 cannot identify the type of the object to be operated, so we need to tell it, there are two ways to solve this problem:

1. Change getchildbyname (CC) to movieclip (getchildbyname (CC), that is, first use getchildbyname to convert the CC string to displayobject, and then change it to movieclip, at this time, as3 recognizes it as a movieclip, so that it can access its attributes and call its methods.

2. Write the statement as follows:

VaR CC: movieclip = mc1

This ["cc"]. xxx

 

There is also a string writing method:

VaR cc = mc1.name

This ["cc"]. xxx

Here, mc1.name must be named in the property panel, rather than mc1.name = "XXX ".

[] Here is the dynamic meaning. The type of the object to be operated is dynamic, so that the as3 compiler can be bypassed.

From: http://blog.sina.com.cn/s/blog_4c4189520100c6qb.html

 

The following sections are self-writtenCode:

// = Create items text =
For (VaR _ item = 0; _ item <6; _ item ++ ){
 VaR txtitem: textfield = new textfield ();
 Txtitem. Text = arritemname [_ item];
 Txtitem. x = 155 + _ item * 85;
 TXT titem. Y = 552;
 VaR O: textfield = new textfield ();
 O = getchildbyname ("txtitem" + _ item. tostring () as textfield;
 O. addeventlistener (mouseevent. Click, createstatisticalgraph );
 Addchild (O );
 Arrobjects. Push (O );
}

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.