3
votes

I started one week ago to study ActionScript 3.0. I would like to do a simple game. It will start with a window displaying a welcome message( "press the button to start"), and a arrow that start from the text and point to the button. I want to create everything from code. I'm using a TextField for the welcome message but I have some trouble. I created a file .fla AIR for Destkop. Then I associated to that file a class called mainFunzioneModidificaTest.as . In this class I wrote a function to set the text of the first window. I use the TextFormat but when I run the .fla file I see the text but without any formatting.The color, the dimension and the font dosen't change Here is the code. Can someone help me? Thank you!

package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;

public class mainFunzioneModificaTest extends MovieClip {

    public function mainFunzioneModificaTest() {
        setText();
    }
    function setText(): void {
        var text: TextField = new TextField();
        var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
        text.setTextFormat(myFormat);
        text.text = "Hello";
        addChild(text);
    }

}

}

1
can you try with var txt: TextField = new TextField(); instead of var text: TextField = new TextField();Jijo Cleetus
I have already tried. It doesn't work. However thank you!SpaghettiFunk

1 Answers

3
votes

You need to set the text first before calling setTextFormat(), or alternatively use text.defaultTextFormat = myFormat;

From the TextFormat documentation:

Use the TextField.defaultTextFormat property to apply formatting BEFORE you add text to the TextField, and the setTextFormat() method to add formatting AFTER you add text to the TextField

Set text before calling setTextFormat():

function setText(): void {
    var text: TextField = new TextField();
    var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
    text.text = "Hello";
    text.setTextFormat(myFormat);
    addChild(text);
}

or set defaultTextFormat:

function setText(): void {
    var text: TextField = new TextField();
    var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
    text.defaultTextFormat = myFormat;
    text.text = "Hello";
    addChild(text);
}