简体   繁体   中英

ActionScript 3.0 function for modify TextField

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

}

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM