What's wrong with this Class?
package { import flash.display.MovieClip; import flash.events.Event; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize;
public class User extends MovieClip
{
public var tf_username :TextField;
public var lb_dptsid :TextField;
public var tf_dptsid :TextField;
public var tf :TextField;
public var tff :TextFormat;
public function User()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
tf_username = createField(18,'Myriad Pro Cond',"0x092846");
lb_dptsid = createField(9,'Myriad Pro Cond',"0x999999");
tf_dptsid = createField(10, 'Myriad Pro Cond', "0x000000");
lb_dptsid.y = 48;
tf_dptsid.x = 26;
tf_dptsid.y = 47;
tf_username.text = "DPTSID";
lb_dptsid.text = "DPTSID";
tf_dptsid.text = "DPTSID";
addChild(tf_username);
addChild(lb_dptsid);
addChild(tf_dptsid);
var tfx:TextField = new TextField();
tfx.x = 0;
tfx.y = 30;
tfx.text = "this one works though.";
addChild(tfx);
}
private function createField(n:Number,f:String,c:String):TextField
{
var tf:TextField=new TextField();
tf.autoSize=TextFieldAutoSize.LEFT;
tf.selectable=false;
tf.embedFonts=true;
tf.defaultTextFormat=getFormat(n,f,c);
return(tf);
}
private function getFormat(n:Number,f:String,c:String):TextFormat
{
var tff:TextFormat=new TextFormat();
tff.font=f;
tff.size=n;
tff.bold=true;
tff.letterSpacing=0;
tff.color=c;
return(tff);
}
}
}
Any TextField created and added to the stage works, but classing things up to be reuseable chokes. What is wrong with my createField function attempts? I get no compile errors, no run-time errors, and nothing drawn to the screen.
You're skipping a step.
You need to give your library font a class name in the linkage. (yes yes, you did that! but you're still skipping a step!)
Then you need to typecast the class to Font:
import flash.text.Font;
var myLetters:Font = new MyriadProCond();
tff.font= myLetters.fontName;
http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/
instantiating the symbol as a var in your object makes the font available to the class.
I know, I know, it's stupid. My guess is they're anticipating html 5 font changes.
You should be removing your event listener after it has fired. just sayin.
yes yes, you did that! Make it a Dwight Yoakam Two-Step skip.
Thanks genius. I thought it would still fire locally without, so thanks for the help.
Wait....edit that. This is an AS only project. So, I have to create an SWF with the font embedded in that?? Fuck. This really sucks.
Well in that case, if you're being all nerdy - you can make a swc file easily in Flex or CS 3/4 and just put your fonts in that. In CS 3/4 just enable swc export in the swf publish settings. Put the swc in your build path in the publish settings of your As project and the font will be available to your AS classes using getDefinitionByName:
import flash.utils.getDefinitionByName; var myLettersClass:Class = getDefinitionByName("MyriadProCond") as Class; var myLetters:Font = new myLettersClass() as Font;
otherwise you can enumerateFonts and just find it on the fly.
I would think @font-face ttf compliance and html 5 will be making all of this easier soon.
Incidentally, although getdefinitionByName looks useful, it's evil. It will not implicitly cause the class to be compiled. Use it only for times like these when you don't know the final class path and you know the class has been compiled by some specific mechanism.
