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

algro picture algro · May 9, 2011 · Viewed 11k times · Source

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?

Answer

Simon Eyraud picture Simon Eyraud · May 9, 2011

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;