Thought this would work...
var bFormat:TextFormat, bStartText:TextField, bQuitText:Textfield;
bFormat = getFormat();
bStartText = getText("Start");
bStartText.defaultTextFormat = bFormat;
bQuitText = getText("Quit");
bQuitText.defaultTextFormat = bFormat;
stage.addChild(bStarText);
stage.addChild(bQuitText);
function getFormat():TextFormat {
var bFormat:TextFormat = new TextFormat();
bFormat.font = "Arial";
bFormat.color = 0X000000;
bFormat.size = 28;
bFormat.align = "center";
return bFormat;
}
function getText(sText):TextField {
var bText:TextField = new TextField();
bText.text = sText;
bText.x = -4;
bText.y = 4;
return bText;
}
Both text fields show up on the stage, however I do not get any of the formatting specified in getFormat(). I've put the code from getFormat() in the main code (not as its own function) and it works fine. Am I passing it incorrectly?
defaultTextFormat
needs to be set before you change the text
. Since you are setting the text
of the TextField inside getText it doesn't have any effect.
Try this :
var bFormat:TextFormat, bStartText:TextField, bQuitText:Textfield;
bFormat = getFormat();
bStartText = getText("Start",bFormat);
bQuitText = getText("Quit",bFormat);
stage.addChild(bStarText);
stage.addChild(bQuitText);
function getFormat():TextFormat {
var bFormat:TextFormat = new TextFormat();
bFormat.font = "Arial";
bFormat.color = 0X000000;
bFormat.size = 28;
bFormat.align = "center";
return bFormat;
}
function getText(sText:String, tf:TextFormat):TextField {
var bText:TextField = new TextField();
bText.defaultTextFormat = tf;
bText.text = sText;
bText.x = -4;
bText.y = 4;
return bText;
}
If you want to change the format of a textfield that was already set you can use setTextFormat
on the TextField
class.