I have a button instance named Button that I have in my movie, this instance has a Dynamic Text object in it named myText, how can I change the text? I already tried Button.myText.text = “Stuff”;
I get the error “Scene 1, Layer ‘Layer 1’, Frame 1, Line 7 1119: Access of possibly undefined property myText through a reference with static type flash.display:SimpleButton.” When I compile.
AS:
import flash.events.MouseEvent;
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void{
TheButton.myText.text = "Moo";
}
You can’t use the dot syntax to access a display object container’s child display objects in AS3 as you did in AS2. Normally you would use the display object container’s
getChildByName()method to get its child display objects, but because your dealing with an instance ofSimpleButtonwhich is a subclass ofDisplayObjectthat method doesn’t exist. The simple solution is to change you button from a button to a movieclip after which the following should work:Note: the
TextFielddisplay object in theTheButtondisplay object container must have an instance name of “myText” and obviously theTheButtondisplay object container must have an instance name of “TheButton”.Also if your going with this approach you may want to rewrite the code as follows:
[UPDATE]
Another solution is to create a movieclip/sprite object for the
SimpleButtonobject’supState,overStateanddownStateproperties like the following: