Code | Games | Geek

Leave a comment

Working embedded fonts in AS3

Embedding a font into AS3 is frustrating and yet another thing I did not find well explained online. You will need to do 3 things:

  1. [Embed] the font file
  2. define the font as a class
  3. register the font

Embed the font from a relative location to your .as file. Since my .as files are in the proj/src folder, I’m using ../art/Intro.otf as the location of my font. You will need to specify embedAsCFF as false, otherwise Flash will give some nasty errors about Flex libraries missing when you export.

Then, in your constructor of your document class, register the font, so that you can use it elsewhere.

After that, you can use the font anywhere in your code. See the code below:

import flash.text.Font;

// font
[Embed(source="../art/Intro.otf", fontName = "Intro", fontFamily="Intro", fontStyle="normal", fontWeight="normal", mimeType="application/x-font", embedAsCFF="false")]

var Intro:Class;

// Constructor
public function myFlash()
    // font setup


// elsewhere
var textf:TextFormat = new TextFormat();
textf.color =           0xFFFFFF;
textf.font =            "Intro";
textf.size =            48;

Enjoy custom fonts!


Leave a comment

Fade in text, then fade out text with embedded font in AS3

There are many snippets online on how to fade, but each with it’s own flaw – some incomplete, some using AS3’s default Tween (yuck). After trying to figure out it for myself, I came up with this. The following is a working example of placing some text on the stage, fading from 0 to 100 alpha, then back to 0.

To start off, import the classes needed. I’m using the TweenLite library, widely regarded as the best (so I’ve heard), better than the default Adobe Tween. After using their website demos, you’ll see it’s much faster. You’ll need to grab the library, and place the com folder from the .zip into the folder with your .fla, as instructed in the Getting Started Guide.

// remember to import
import com.greensock.*;
import com.greensock.easing.*;

import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.text.Font;

Continue reading