Dynamic TextFields in Actionscript 3.0
Posted by stephen on September 25th, 2008I often have issues getting dynamic text to show up at all when I want to animate it in some way. If the text field is created statically, you just have to embed the font in the properties dialog. Here’s how to do it with text fields created dynamically…
- Add the desired font to your library and export it as a class. I’ll use Arial for this example.
- Right click on the library item and choose “Linkage”.
- Make sure Export for ActionScript and Export in first frame are checked.
- The Base class field should be “flash.text.Font”.
- The Class field should be the name of your font (”Arial”). Not sure how this works with spaces, but I would not use them just to be safe.
- Create your TextFormat.
- var myFormat = new TextFormat();
- myFormat.font = new Arial().fontName; //use the font class name
- myFormat.color = 0xA2BCD5;
- myFormat.size = 12;
- Create your TextField.
- var myTF:TextField = new TextField();
- myTF.selectable = false;
- myTF.embedFonts = true;
- myTF.defaultTextFormat = showOnHomeFormat;
- myTF.setTextFormat(showOnHomeFormat);
- myTF.text = “Here is my text!”;
- Create a sprite container for the text field.
Note: This is only necessary for animating the text or making it a button.- var mySprite:Sprite = new Sprite();
- mySprite.addChild(myTF);
- Add the sprite to the stage.
- root.addChild(mySprite);
For a more complete list of TextFormat/TextField properties and methods, visit the following links:
Recent Comments