Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Align TextField to Stage Center with Flash ActionScript 3

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

So far so good. Let's try to to align the TextField to the center of the stage of the Flash Movie with Flash ActionScript 3. Of course the Text Field can be aligned to any position on the stage of the Flash Movie you like.

Flash ActionScript Codes:

// Add Content to Text Field
textField_txt.text = "Hello World";

 

// Autosize TextField with the text and align to CENTER.
textField_txt.autoSize = TextFieldAutoSize.CENTER;

 

//////////////////////////////////////////////////////////
///// Align TextField to Stage Center /////
/////////////////////////////////////////////////////////

 

// Find the center co-ordinate of Stage
var stageCenter_x:Number = stage.stageWidth/2;
var stageCenter_y:Number = stage.stageHeight/2;

 

// Find the center co-ordinate of TextField
var textCenter_x:Number = textField_txt.width/2;
var textCenter_y:Number = textField_txt.height/2;

 

// Align TextField to Center
// Note: textField_txt.x and textField_txt.y is the Top Left corner of TextField
textField_txt.x = stageCenter_x - textCenter_x;
textField_txt.y = stageCenter_y - textCenter_y;

Download Flash Source File:

Flash Source File hello-world-3.fla

Remarks:

This is the end of the first series of Flash ActionScript 3.0 tutorial.