Working with static text

Static text is created only within Adobe Animate (formerly named Adobe Flash Professional). You cannot programmatically instantiate static text using Haxe code. Static text is useful if the text is short and is not intended to change (as dynamic text can). Think of static text as similar to a graphic element like a circle or square drawn on the Stage in Adobe Animate. While static text is more limited than dynamic text, OpenFL does allow you to read the property values of static text using the StaticText class.

Accessing static text fields with the StaticText class

Typically, you use the openfl.text.StaticText class to interact with a static text instance placed on the Stage. You can't instantiate a static text instance programmatically. Static text is created in Adobe Animate.

To create a reference to an existing static text field, iterate over the items in the display list and assign a variable. For example:

for (i in 0...this.numChildren; i++) {
    var displayitem:DisplayObject = this.getChildAt(i);
    if (Std.isOfType(displayitem, StaticText)) {
        trace("a static text field is item " + i + " on the display list");
        var myFieldLabel:StaticText = cast(displayitem, StaticText);
        trace("and contains the text: " + myFieldLabel.text);
    }
}

Once you have a reference to a static text field, you can use the properties of that text field in Haxe. The following code assumes that a variable named myFieldLabel is assigned to a static text reference. A dynamic text field named myField is positioned relative to the x and y values of myFieldLabel and displays the value of myFieldLabel again.

var myField:TextField = new TextField();
addChild(myField);
myField.x = myFieldLabel.x;
myField.y = myFieldLabel.y + 20;
myField.autoSize = TextFieldAutoSize.LEFT;
myField.text = "and " + myFieldLabel.text

results matching ""

    No results matching ""