Search code examples
flashactionscript-3textfieldstage

How to change textfields on stage from an external class in as3


I tried to change the textfield on the stage from an external class but it doesn't work. Thats the code how I tried it:

package  
{
    import flash.display.*;
    import flash.text.TextField;

    public class Exp extends Sprite
    {
        public function Exp() 
        {
            trace(stage.getChildByName("abc"));
            TextField(stage.getChildByName("abc")).text = "abc";

        }

    }

}

On my stage I got a textfield wich is dynamically with the instancename: "abc". But everytime I start the program flash tells me stage.getChildByName("abc") would be a null-object.

I hope someone can help me.


Solution

  • I've never used getChildyName before.. You can just use this:

    var rt:MovieClip = MovieClip(root);
    trace(rt["abc"]);
    

    Or shorter:

    trace(MovieClip(root)["abc"]);
    

    If your example is your document class -

    package
    {
        import flash.display.MovieClip;
        import flash.text.TextField;
    
        public class Exp extends MovieClip
        {
    
            public function Exp()
            {
                var r:MovieClip = MovieClip(root);
    
                TextField(r["abc"]).text = "abc";
            }
        }   
    }