Why can't set "defaultTextFormat" directly? - actionscript-3

for example,we can set graphics of a shape directly(without creating an external Graphics variable):
var my_shape:Shape=new Shape();
my_shape.graphics.beginFill(0);
but that's not same as defaultTextFormat
the below code doesn't work:
var my_text:TextField=new TextField();
my_text.defaultTextFormat.size=47;
typing dot after defaultTextFormat,the code hint of text format appears and there is no compiler error but still doesn't work
we must create an external TextFormat variable:
var my_text:TextField=new TextField();
var my_format:TextFormat=new TextFormat();
my_format.size=47;
my_text.defaultTextFormat=my_format;
but why can't set directly?
I don't like a lot of variables.
after that,explain the difference between textFormat and Graphics.
Thanks for your help.

When you access/read a TextField's defaultTextFormat property (which is what's happening in the line my_text.defaultTextFormat.size=47;), you end up getting a whole new object returned. Eg, it creates a new TextFormat and returns that.
Here is an example to illustrate:
var tf:TextFormat = new TextFormat();
textField.defaultTextFormat = tf;
trace(tf == textField.defaultTextFormat) //false
The TextField doesn't know anything about the TextFormat it returns from defaultTextFormat. So when you change it, it doesn't update anything automatically because it has no scope inside the TextField that generated it.
In order to see the change, you have to reassign the whole object, and
then reassign the text (if you've already set the text).
This unfortunately means you'll have do it like in your second example.
It's probably some kind of an efficiency thing under the hood to help prevent memory leaks and the like.
Here are some examples to consider further:
var txt:TextField = new TextField();
addChild(txt);
var tf:TextFormat;
txt.text = "hi"; //default formatting;
tf = txt.defaultTextFormat; //get the default formatting, which actually returns a brand new object
tf.color = 0xFF0000; //make it red;
//nothing has changed visually
txt.defaultTextFormat = tf; //this won't update it either
//nothing has changed visually
txt.text = txt.text; //now that we've 'changed' the text, you'll see red

my_text.defaultTextFormat = my_format;
my_format is the defaultTextFormat of your textField my_text. defaultTextFormat is a property of your TextField (which value is my_format).
my_format.size = 47;
47 is the size of your TextFormat my_format. size is a property of your TextFormat (which value is 47).
my_text.defaultTextFormat.size = 47;
...but size has never been a property of a defaultTextFormat.
So you cannot put properties directly on the defaultTextFormat. What
you need to do is to make a text format, set the properties, THEN set
defaultTextFormat = myTextFormat.
Adobe help about defaultTextFormat.

Related

AS3 How to keep text box in one frame

I'm trying to add a few text boxes so that people can select text and copy it to their clipboard.
I wrote the following, but the problem is that the text box stays through all the frames after it's been accessed and I just want it to stay in that frame. Do I have to set them on invisible elsewhere? How come other frames even have access to this code?
Any solutions? Thanks!
stop();
var myFont = new fontCandy();
var myFormat:TextFormat = new TextFormat();
myFormat.size = 10;
myFormat.font = myFont.fontName;
myFormat.color = 0xFFFFFF;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.embedFonts = true;
myText.antiAliasType = AntiAliasType.ADVANCED;
myText.text = "my#email.com";
addChild(myText);
myText.border = true;
myText.wordWrap = true;
myText.width = 100;
myText.height = 15;
myText.x = 70;
myText.y = 185;
In FlashPro, anything done in code is not subject to timeline keyframes. Something done in code will persist until it's scope (the timeline it's on) is unloaded or you've explicitly cleaned it up via code. (though certain things can keep your objects in memory even after this).
This also includes display objects that originated on the timeline (not through code) but have had their parentage or positioning manipulated via code (so if you use addChild/setChildIndex on a timeline object)
You have to explicitly remove it by using removeChild(instanceOrVarName);
So in your case: removeChild(myText); will remove that text field.
So, to sum it up:
Things done through code must be cleaned up through code.
Here are a few options for you to explore:
call removeChild(myText) on the very next frame
listen for the ENTER_FRAME event and check if the playhead has moved, if so then do removeChild(myText) and remove the listener.
on the frame you have this code, put a text field on the stage (just on that frame), give it an instance name of myText, then keep the code you have except for the new TextField() line, and the addChild line. This should make it do what you want but also let it be removed on the next frame.

Formatting Dynamic textfieldtype.DYNAMIC in AS3

Hi I am trying to format a textfield called myResult with the following code. It seems to have no effect on the size or type of text when I attempt it. Is there another way to format the textfield or am I missing something. The textfieldtype is DYNAMIC as it is displaying a result. Here is the code I am using
var myFormatA:TextFormat = new TextFormat();
myFormatA.color = 0xAA0000;
myFormatA.size = 28;
myFormatA.italic = true;
myFormatA.align = TextFormatAlign.CENTER;
myResult.setTextFormat(myFormatA);
use defaultTextFormat to apply a default format to any text that will be displayed (in the future if you will) in your textfield. Use setTextFormat to format the text that is currently displayed in your textfield. This is an important difference.

AS3 change only font of TLFTextField

To change font in TLFTextField I need to do something like this:
var textFormat:TextFormat = new TextFormat();
textFormat.font = "NewFontName";
textField.defaultTextFormat = textFormat;
and it works ok, but I loose all other previous properties of textField like color, font size or align. How can I overcome this? I don't want new TextFormat, I want to change font only in existing one. I set color and align in fla file and I want to change font in AS code.
Easiest approach is:
var format: TextFormat = textField.defaultTextFormat;
format.font = "Consolas";
textField.setTextFormat(format);
Or you can store reference on TextFormat, if you change it often

How to get current active stage?

Is there any way to do something like this?
var text:TextField = new TextField();
text.type = TextFieldType.INPUT;
getCurrentActiveStage().focus = text;
If you have any display object added to the display chain in some way, you can reference it through the stage property. So, if text is already on the stage, just call:
text.stage.focus = text;
If not, you can't access the stage without some reference to it.
You can access stage from any place by FlexGlobals.topLevelApplication.stage

AS3: Embedding characters

I having some trouble with TextFields and caracter embedding. As I have understood, the way to embed character in Flash, is to have a TextField in a movieclip that is exported to actionscript via some classname. Then have the TextField embed the characters.
But when i try to use that TextField in my project, I cannot auto resize the field any longer!? Is there a better way to embed charactes? or am I missing some unknow attribute? (and yes i have tried TextField.autoSize = "left" (or "center" or "right")).
The TextField is configured like this in Flash CS4:
Properties:
http://screencast.com/t/0VB6KnNO6G
Library implementation:
http://screencast.com/t/w3yQLqit0veI
And I embed the MovieClip containing the TextField like this:
protected var tabname:MovieClip = new Text(); // The property on the object
Adding the text and setting its Settings:
var txt:TextField = tabname.txt;
if( !contains(tabname) )
{
addChild(tabname);
var format:TextFormat = new TextFormat();
format.bold = true;
format.font = "Arial";
format.size = 12;
format.align = "left";
var dropShadow = new DropShadowFilter(0);
dropShadow.color = 0xFFFFFF;
dropShadow.strength = 2;
dropShadow.blurX = dropShadow.blurY = 5;
dropShadow.alpha = .7;
txt.type = TextFieldType.DYNAMIC;
txt.multiline = tabname.wordWrap = false;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.defaultTextFormat = format;
txt.filters = [dropShadow];
txt.mouseEnabled = false;
txt.x = 10;
}
txt.text = value;
txt.y = Math.ceil((tabmask.height - txt.height) /2);
To embed fonts, don't rely on wrapping them in MovieClips in the library. They should be embedded correctly as Fonts. I have included some basic steps below for embedding fonts, then an example for your particular situation:
1 - Make the textfield Dynamic and click the Embed.. button
2 - Name the font with something meaningful (like the fonts name) and tick the character sets you will be using (usually I select caps, lowercase, numbers and punctuation). Also note the Style is 'Bold', you will need to embed a font set for each style. So if you want to use Bold and Regular, you need to embed 2 fonts.
3 - If you plan on adding textfields dynamically through ActionScript, goto the ActionScript tab and add a class for it (again, use a meaningful name)
4 - Finally click ok, and away you go. I have setup an example, using these steps, and the auto size method, you can see the results below
In Flash, you can click the [Embed...] button below the TextField's character properties. In the window that you get then, you can specify which characters you want embedded in your textfield.
There's a lot more to say about font embedding but this is the simple story. Flash CS5 added TLF TextFields but I don't think you were referring to those, right?
The autoSize property really has nothing to do with font embedding but I guess your TextField is not Dynamic when you cannot auto resize it?
Are you using CS5 or CS4 or earlier by the way?