Align the content of a TextField, but keep its width x height

Alexander Farber picture Alexander Farber · Mar 16, 2012 · Viewed 17.1k times · Source

In a card game I use a TextField in the middle to display the playing table number, but also to detect if a playing card has been played - using myTextField.hitTestObject(myCard) - which means the TextField's position and dimensions may not change:

enter image description here

My current AS3 code is:

        var format:TextFormat = new TextFormat();
        format.color = 0xFFFFFF;
        format.size = 30;
        format.bold = true;

        myTextField.defaultTextFormat = format;
        myTextField.border = true;
        myTextField.borderColor = 0xFFFFFF;
        myTextField.x = W/2-Card.W/2;
        myTextField.y = Card.H;
        myTextField.width = Card.W;
        myTextField.height = Card.H/4;

        addChild(myTextField);

However the TextField's content (the String "#2029" in the above screenshot) is not in the center of it.

I can not set _middle.autoSize = TextFieldAutoSize.CENTER because this changes the width of the border and breaks hitTestObject().

Is there another way to align the text in the middle please?

Answer

taskinoor picture taskinoor · Mar 16, 2012

Set the align property of TextFormat.

format.align = TextFormatAlign.CENTER;