Question

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.

Was it helpful?

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";
        }
    }   
}

OTHER TIPS

I know an answer has already been accepted for this question but I think this is a better one:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;

    public class Main extends Sprite 
    {
        private var _textField:TextField;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var textField:TextField = new TextField();
            textField.name = "abc";
            stage.addChild(textField);

            _textField = TextField(stage.getChildByName("abc"));

            trace(_textField.name); // output: abc

        }// end function

    }// end class

}// end package
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top