setting textfield vertical alignment, AS3 - actionscript-3

textFormat.align = "center";
This, but a vertical equivalent please?
Because of my experience with HTML I have tried
textFormat.valign = "middle";
But it does not work

You can't align text vertically automatically in AS3.
You'll have to write your own function to do this.
eg.
public function verticalAlignTF(tf:TextField):void
{
tf.y += Math.round((tf.height - tf.textHeight) * .5);
}
Another option would be using TLFTextField, which has a verticalAlign property. Just note that TLF has been deprecated by Adobe, so you might run into some issues.

Related

Vertical align text As3 Label Coponent

I have a simple question, is it possible to vertical align the text that is displayed by the Label component in Flash CS6.
Only the TLFTextField (class in the package fl.text) has a built-in property to set the text vertical alignment.
Go to this page for more information
Unfortunately in AS3 labels are very limited in what they can do - you could use the setStyle property of the Label to reference a predefined TextFormat but still TextFormat lacks any appropriate public properties that are relevant to your question.
So I advise you just to use a textfield as a replacement for the label's text option which you can easily manipulate programatically like so:
var label1:TextField = new TextField();
label1.y = 100; // any value here for vertical text alignment
label1.width = 100; // any height
label1.height = 100; // any height
label1.text = "label 1 text"; // label text here
addChild(label1); // adds it to the stage
EDIT: added addChild - totally forgot!
Here is a simple tutorial about AS3 textfields

AS3 - Changing size of Label on Button component

I would like to increase the size of the label on a Button in flash. The label seems to only be about 3/4 the width and 3/4 the height of the button. I would like to make the label be the full width and height of the button.
The label field is just a string, and changing the width/height on the textField property of the button does not seem to change anything. Also textFormat doesn't have options for changing text width/height.
I'm out of ideas. Any help would be greatly appreciated!
The only way I know is to do it via code.
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.size = 20
var myButton:Button = new Button();
myButton.label = "Click Me";
myButton.setStyle("textFormat", myTextFormat);
myButton.setSize(120, 60);
myButton.x = 0;
myButton.y = 0;
addChild(myButton);
After much reading, I found a few that might help future viewers of this question. I am using AS3, CS5.5. 'bw' is the instance name of the button. These can be used if you are using a button 'Component'.
bw.setStyle("textFormat", new TextFormat("Verdana", 20, "bold", "italic", "underline", true));
bw.label = "Dog Snacks"; // can be also set via properties, but this is handy if you want the text to change after clicking
bw.setSize(280, 30); // can also be set via properties

How do I change the width of a TextField without stretching the text?

I used to know how to do this, so, I KNOW it's possible, but I can't figure it out again. I'm altering the width of my TextField by setting the width property but that warps the text. I want to alter the width of the text field WITHOUT altering the way the font looks (obviously). I believe it has something to do with autoText or some such idiocy (why would I EVER want to warp my text?!) but I just can't recall.
myField.width = 100; // if the original width was 50 this simply stretches the field to 100, rather than adding 50 pixels into which characters can be drawn.
TIA
In the IDE, you need to
a) Double-click on the text-field. This takes you into the editing mode for the text field. Then resize it.
or
b) select the text tool and then resize it.
Resizing it using the transform tool will increase the width as if it were a shape (which is PRETTY useful in some animations, so it isn't really that idiotic)
I am guessing your problem is TextField.defaultTextFormat.
just setup a TextFormat object then on your text field setup the default text format and it should keep the text format no matter what you do to it.
You see when you change pretty much anything about a text field the text formatting gets reset and you have to reapply it. However if you setup the default text format it will take care of that automatically.
Here is a dirty little prototype.
package src
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var tx:TextField;
private var txf:TextFormat;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initMain);
}
private function initMain(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initMain);
// setup a text format so you can keep your text the same all the time.
txf = new TextFormat('Arial', 12, 0x000000);
tx = new TextField();
tx.width = 50;
tx.text = "I want this text not to wrap so it will be resized at runtime."
// Turned this on for testing purposes only.
tx.wordWrap = true;
tx.defaultTextFormat = txf; // This line is the x factor most likely.
tx.x = 100;
tx.y = 100;
addChild(tx);
stage.addEventListener(MouseEvent.CLICK, toggleTextFieldSize, false, 0 ,true);
}
private function toggleTextFieldSize(e:MouseEvent):void
{
if (tx.width == 50)
{
tx.width = 400;
}
else
{
tx.width = 50;
}
}
}
}
Hope this is what you were looking for.
you are in the IDE right?
try doubleclicking the textfield and then use the box on the right side of the textfield to resize the whole thing without strechting the text.
** edit **
here is a code example of how resizing a TextField works:
http://wonderfl.net/c/qbDv
but this only works with dynamic TextFields. TF created with code are either DYNAMIC or INPUT but inside the IDE you can create TF that are STATIC and those can't be resized via ActionScript. So you have to change the TextField's type or create the TF via script.
Another solution would be to use a wrapper for the text field, like a Group element, and just increase its width.
You could also use a textformat and assign the left and right margin
myTextFormat.leftMargin = some number
and
myTextFormat.rightMargin = same number
as left margin.

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?

Rotate a sprite using ActionScript3

I want to rotate a sprite in 3d using AS3. The example below, tells how to do rotate an image using MXML and AS3, however, I want to do it through pure AS3:
Example
thanks
Flex is AS3. Flex compiles down to actionscript. Often, it's just a declarative (as opposed to the imperative) way to get things done.
So the meat of that example is in the code snippets:
private function playEffect(target:Animate, angle:Number):void {
if (!target.isPlaying) {
rotY += angle;
target.play();
}
}
//snip...
<fx:Declarations>
<fx:Number id="rotY">0</fx:Number>
<s:Rotate3D id="fxRotate3DNeg" target="{image}" angleYTo="{rotY}"
autoCenterTransform="true" />
<s:Rotate3D id="fxRotate3DPos" target="{image}" angleYTo="{rotY}"
autoCenterTransform="true" />
</fx:Declarations>
What's doing the work is the "Animate" object in conjunction with the two "Rotate3D" objects. So to get this to work in pure AS3, the only tough thing that's required is linking to the flex libraries. Depending on your IDE, that's pretty easy to do.
From there all you have to do is create the objects you want, imperatively instead of declaratively. So instead of doing things like:
<fx:Number id="rotY">0</fx:Number>
You need to do:
var rotY:Number = 0;
Once you know that, converting from Flex to AS3 and vice versa is pretty straightforward. The translated flex code would look something like the following in ActionScript:
import spark.effects.Rotate3D;
var rotY:Number;
var fxRotate3DNeg:Rotate3D;
var fxRotate3DPos:Rotate3D;
rotY = 0;
fxRotate3DNeg = new Rotate3D(image);; //the constructor sets the "target" property
fxRotate3DNeg.angleYTo = rotY;
fxRotate3DNeg.autoCenterTransform = true;
fxRotate3DPos = new Rotate3D(image);
fxRotate3DPos.angleYTo = rotY;
fxRotate3DPos.autoCenterTransform = true;
Now, that's off the top of my head, glancing at the Rotate3D API and typing in this text editor so I'm sure it's not perfect but it should give you a clear idea on how to move forward. If you need more help, let me know and I could translate more of the example.
I hope that helps,
--gMale
EDIT:
As I look at the code, one other tricky point is that the angleYTo properties are bound to rotY. So to truly get this to work, you have to explicitly set those properties in the playEffect function. As in:
private function playEffect(target:Animate, angle:Number):void {
if (!target.isPlaying) {
rotY += angle;
//manually set properties
fxRotate3DNeg.angleYTo = fxRotate3DPos.angleYTo = rotY;
target.play();
}
}
Alternatively, you could imperatively create the data binding, which is pretty easy to do. Then, the playEffect function would require no modification.
Its like rotating the object as you usually do. However in 3d space you will have to use:
sprite.rotationY
Make sure you are exporting for flash 10 or later since the 3d functionality doesnt exist in earlier versions.