Try to mask a textfield in actionscript - actionscript-3

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);

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"

Using text as a mask to add a gradient to the text in AS3

I want to apply a gradient to my text in Flash Develop and read that using a mask is the easiest way. Problem is that I can not get it to work. I tried to create a mask with just the textField and by adding it to a sprite, neither work.
Another strange thing is that the textmask will function as a mask for start title if I reverse them like:
starttitle.mask =textmask
When I try the code below nothing displays.
var format2:TextFormat = new TextFormat();
format2.size = 40;
format2.font = "Times New Roman";
format2.bold = true
starttitle = new TextField;
starttitle.defaultTextFormat = format2;
starttitle.text = "Memory Circles";
starttitle.textColor = 0xFF0000
starttitle.autoSize = "center";
starttitle.background = false;
starttitle.backgroundColor = 0xFF4242;
starttitle.x = (300 - starttitle.width * .5);
starttitle.y = (150 - starttitle.height * .5);
starttitle.mouseEnabled = false;
addChild(starttitle)
var textmask:Shape = new Shape
var fillType:String = GradientType.LINEAR;
var colors:Array = [0xFF0000, 0];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
var gradientBoxMatrix:Matrix = new Matrix();
gradientBoxMatrix.createGradientBox(50,100,45,0,0);
var spreadMethod:String = SpreadMethod.REPEAT;
textmask.graphics.beginGradientFill(fillType, colors, alphas, ratios,
gradientBoxMatrix, spreadMethod);
textmask.graphics.drawRect(200,100,300,100)
addChild(textmask)
textmask.mask =starttitle
I read something about the font needing to be embedded but I'm using Times New Roman and thought this would be embedded. Also other fonts don't work.
Try to use bitmap caching on starttitle and textmask:
starttitle.cacheAsBitmap=true;
textmask.cacheAsBitmap=true;

AS3 - setTextFormat(), defaultTextFormat not working as designed?

UPDATED with simpler snippet
I am building a Flash swf to check through some XML files using AS3.
I only have access to the Flex compiler, not the actual Flash IDE or Flashdevelop, so all my xml checking code is a single AS file, compiled into an swf. In order to display information, I create and add a TextField and call appendText() (I use += in this snippet).
The snippet below seems to not set the new format on existing text. It appears to not have any effect whatsoever on the text.
I was under the impression that setTextFormat would change the existing text and that any new text added after calling setTextFormat would use the defaultTextFormat object. This does not appear to be the case in my code. Am I incorrect in my understanding?
public function AS3Tester()
{
display_txt = new TextField();
display_txt.multiline = true;
display_txt.width = 1024;
display_txt.height = 768;
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.font = "Lucida Console";
display_txt.defaultTextFormat = tf;
display_txt.text = "Text Before setTextFormat\n"; //Should use default style tf
addChild(display_txt);
var tf2:TextFormat = new TextFormat();
tf2.size = 18;
tf2.color = 0xFF0000;
display_txt.setTextFormat(tf2);
display_txt.text += "Text after setTextFormat\n"; //previous line should use tf2, this new line should use default tf
}

Text Field in Actionscript3

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;

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide