Text Field in Actionscript3 - actionscript-3

I am trying to create a text field manually from actionscript alone. I got parts of it but now i don't know how to give the fonts size and different fonts can somebody tell me how to go about this?
for (var i:Number = 0; i < weekdaystringArray.length ; i++)
{
weekdaystring.text = weekdaystringArray [i];
weekdaystring.x = 150;
weekdaystring.y = 994;
addChild (weekdaystring);
weekdaystring.textColor = 0xFFFFFF;
}
This is what I have so far...

You need to apply a TextFormat to the TextField with the setTextFormat() method.
var format:TextFormat = new TextFormat();
format.font = "Courier";
format.size = 24;
// then inside your loop:
weekdaystring.setTextFormat(format);

it need to apply a TextFormat to the TextFiled
var format:TextFormat = weekdaystring.defaultTextFormat;
format.font = "courier";
format.size = 24
weekdaystring.defaultTextFormat = format;

Related

How can I change the size in TextField AS3?

I'm doing a game and would like to increase the size of the score since it's too small
Thanks for the answer
This is the code for the score:
var score_txt:TextField=new TextField();
score_txt.textColor=0xFFFFFF;
score_txt.x=142,3;
score_txt.y=563,05;
note:the score must be TextField not TextFormat
have you tried this?
score_txt.width = 100;
score_txt.height = 100;
Using a TextFormat could be easier:
var myFormat:TextFormat = new TextFormat();
myFormat.size = 15;
score_txt.defaultTextFormat = myFormat;
score_txt.text = "your text"

AS3 Why does editing a Textfield inside of a MovieClip throw all the sizes off?

I'm pretty frustrated with AS3 right now. It seems like there must be serious issues with how things scale inside of movieclips. From the stage everything seems to work fine.
On one of my projects I try to set a textfield.width equal to its container(movieclip)
tf.width = mc.width; Of course it says everything is even but when I look at the border of the textfield it's nowhere near the size of the movie clip it's contained within.
While trying to make a much smaller sample to share with you guys for help, that part worked, but trying to resize text did something completely different. Can anyone help me make sense of all this? The code below seemingly randomly starts changing the size of everything.
Also, I was trying to follow this code from here but just kept getting an error when trying to change the format size in a while loop: Autosize text to fit the width of a button
var square:MovieClip = new MovieClip();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = 0;
square.y = 0;
square.width = 200;
square.height = 200;
var tffSize = 400;
var tffOr:TextFormat = new TextFormat();
tffOr.size = tffSize;
tffOr.align = TextFormatAlign.CENTER;
var tf:TextField = new TextField();
square.addChild(tf);
tf.defaultTextFormat = tffOr;
tf.text = "Hello";
tf.border = true;
tf.width = square.width;
tf.height = square.height;
trace(tf.textWidth);
trace(square.width);
while (tf.textWidth > square.width || tf.textHeight > square.height)
{
trace("too Big");
newTFF();
trace(tf.textWidth + " vs " + square.width);
square.width = 200;
trace(tf.textWidth + " vs " + square.width);
}
function newTFF()
{
tffSize = tffSize - 1;
var tff:TextFormat = new TextFormat();
tff.size = tffSize;
tff.align = TextFormatAlign.CENTER;
//tf.defaultTextFormat = tff;
tf.setTextFormat(tff);
tf.autoSize = "left";
}

Try to mask a textfield in actionscript

I trying to mask a textField with the following code, but it's not working.
var Blue_clip:blue_img = new blue_img()
addChild(Blue_clip)
var field:TextField=new TextField();
var format:TextFormat=new TextFormat();
format.size = 40;
format.font = "Arial";
field.defaultTextFormat = format;
field.text = "Hello World";
addChild(field);
Blue_clip.mask = field;
Please guide me.
Method 1) - Use runtime AS mask
_root.attachMovie("mask", "mask_instance", 3 ,{_x:10, _y:20});
_root.masked_content_mc.setMask(mask_instance);
where mask is AS exported movie clip in lybrary (simple shape 100x100px for example)
Method 2) - Embed font.
You have to embed the font using Text->Font Embedding and then set it to the text field:
var style_tf:TextFormat = new TextFormat();
style_tf.font = "ArialEmbed";
style_tf.size = 16;
_root.my_text.setTextFormat(style_tf);

distortion in textField

I have a bitmap data which draw a text field. after scaling, text are distortion.
I using following code:
// tf is text Field and bm is bitmap.
var tf:TextField = new TextField();
tf.text = "Hello world";
var bd:BitmapData = new BitmapData(200, 200, true, 0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Please guide me.
You should use transform matrix to draw an upscaled text field (or any other vector graphics object) onto a BitmapData.
var mat:Matrix=new Matrix();
mat.scale(2.0,2.0);
bd.draw(tf,mat);
First, increase the font size and then convert and scale as bitmap like so,
//---- Text format ----
var textFormat:TextFormat = new TextFormat();
textFormat.font = "Arial";
//textFormat.bold = true;
textFormat.size = 40;
//---------------------------------------------
//
var tf:TextField = new TextField();
tf.text = "Hello world";
/*tf.antiAliasType = AntiAliasType.ADVANCED;
tf.gridFitType = GridFitType.PIXEL;
tf.thickness = 0;
tf.sharpness = 0;*/
tf.setTextFormat(textFormat);
//
//
var bd:BitmapData = new BitmapData(200,200,true,0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
bm.smoothing = true;
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Best luck.

Change font-color of TextField inside MovieClip

I've a problem to change the font-color of a TextField after it has been added as a child to a MovieClip.
My code:
var btn:MovieClip = new MovieClip();
// other code for button
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.bold = true;
tf.font = "Arial"
tf.color = 0x000000;
var capt:TextField = new TextField();
capt.defaultTextFormat = tf;
capt.width = 200;
capt.height = 50;
capt.text = "Test";
capt.y = 20;
capt.x = 20;
capt.border = false;
capt.selectable = false;
btn.addChild(capt);
// .....
How can I Change the font-color after the last line?
It sounds like you're looking for TextField.setTextFormat(). You can either adjust your original TextFormat or just make a whole new one.
tf.color = 0xFF0000;
capt.setTextFormat(tf);
Assuming the TextField falls out of scope after that last line (you don't show enough to know if it does or not), you'll need to loop through the button to get the TextField and do it from there.
var i:uint;
var l:uint = btn.numChildren; //numChildren and length are 'heavy' getters, never use them as restrictors in a loop
for ( i = 0; i < l; i++ ) {
var cur:DisplayObject = btn.getChildAt( i );
if ( cur is TextField ) {
( cur as TextField ).setTextFormat( /*set new TextFormat in here*/);
break;
}
}
That assumes there is only the single TextField, of course. If there are multiple, I would extend MovieClip and add a public property for the value you want to change.
You can also just use the textColor property of a textfield:
capt.textColor = 0xFF0000;