AS3 Gotcha #1: “autoSize” for Dynamic Text Fields Is No Longer the Friend You Thought It Was

(To be clear: it’s still a friend. It just has a new attitude.)

So, apparently the “autoSize” parameter for dynamic textfields is no longer a boolean!

This caught me off guard when I was doing what I usually do when I run out of space in a dynamic text field:

label_txt.autoSize = true;

But if you try to do this in AS3, you’ll get an error #1067 (a compiler error to be specific), which says:

“Implicit coercion of a value of type Boolean to an unrelated type String.”

...which in designer-speak means You Obviously Don’t Know What the Hell You’re Doing, So Why Don’t You Scoot Your Tuckus On Over to the AS3 Language Reference, Dufus.

Well, then.

Cute-siness aside, what it actually says is that ”autoSize” is a String, so, why you tryin’ to force a Boolean value (true or false) onto it and violate is own nature, fool?

So after some hunting in the handy dandy AS3 Language Reference

The good news is now in ActionScript 3.0, you have four choices (count ‘em, four!) instead of just two before, and they basically mean what you think they mean:

label_txt.autoSize = TextFieldAutoSize.NONE — The default. No resizing.

label_txt.autoSize = TextFieldAutoSize.LEFT — Will automatically resize the textfield and left-align the text.

label_txt.autoSize = TextFieldAutoSize.CENTER — Will automatically resize the textfield and center the text.

label_txt.autoSize = TextFieldAutoSize.RIGHT — Will automatically resize the textfield and right-align the text.

And there you have it. I’ll be posting more AS3 Gotchas as I come across them, as a public service to all my fellow Flash designers and developers making the proverbial journey into ActionScript 3.0 from its previous iterations. In theory, the transition is easy, but I have definitely found that it’s the niggly-wiggly things like this that getcha and can drive you crazy to the point of...that’s right...pissing you off.


About this entry