Imagine you have a custom button class and you create a new button:
_myButton = new CustomButton();<br /> _myButton.name = "myButton";<br /> addChild(_myButton);
You have some interaction with other objects and you need to perform some action on the button. So you try:
myButton.enabled = false;
» This line will throw an error (1120: Access of undefined property…) because the name property can’t be used as an instance name. It’s just a string of text.
So… how to address the button?
You need to create a new DisplayObject since getChildByName() returns one.
Adobe LiveDocs on the DisplayObject’s name:
“Indicates the instance name of the DisplayObject. The object can be identified in the child list of its parent display object container by calling the getChildByName() method of the display object container.”
The following code works:
var myButton:DisplayObject = getChildByName("myButton");<br />
myButton.enabled = false;
If you need to address the button inside an event handler, you would write something like this:
myButton.addEventListener(MouseEvent.MOUSE_UP, buttonChangeHandler);<br />
private function buttonChangeHandler(event:MouseEvent):void<br />
{<br />
var myButton:DisplayObject = getChildByName(event.target.name);<br />
myButton.enabled = false;<br />
}