Search code examples
actionscript-3inputfocustextinputsetfocus

How set the focus of a input-TextField without having a stagereference?


I got a text-InputField:

var textfield:TextField = new TextField();
textfield.text = "";
textfield.type = TextFieldType.INPUT;
addChild(textfield);

because this textfield don't reside in the main-class, I don't have a stage-reference for doing:

textfield.stage.focus = textfield;
or 
stage.focus = textfield

How can I force the textfield to display the blinking line at position zero?


Solution

  • You can add a static reference to the stage in your main class and set focus through it (or create a dedicaced class to get stage reference wherever you are).

    Main class code :

    public class MainClass extends Sprite{
        public static var STAGE : Stage;
    
        public function MainClass(){
            STAGE = stage;
        }
    }
    

    Textfield class code :

    MainClass.STAGE.focus = myTextfield;