11.22. Using Tables to Arrange Form Elements

Problem

You want to use ActionScript to arrange the elements of a form on the screen.

Solution

Create and use a custom Table class.

Discussion

You can use ActionScript to control the position of any graphical object such as a movie clip, a button, or a text field, using the _x and _y properties. Additionally, each graphical object has properties that return its height and width (_height and _width). You can use these properties in concert to position instances on the Stage relative to one another.

For example, we can position a movie clip horizontally such that its x coordinate is five pixels to the right of a text field. The text field’s _x property plus its _width property yields the x coordinate of the right edge of the text field. In this example, we add five pixels to allow for spacing between the movie clip and the text field. We also align the top of the movie clip with the top of the text field.

myMovieClip_mc._x = myTextField_txt._x + myTextField_txt._width + 5;
myMovieClip_mc._y = myTextField_txt._y;

In this example, we position the movie clip such that its y coordinate is five pixels below the bottom of the text field. This example is similar to the preceding one but uses the _y and _height properties to calculate the vertical position. We also align the left edges of the movie clip and the text field.

myMovieClip_mc._y = myTextField_txt._y + myTextField_txt._height + 5;
myMovieClip_mc._x = myTextField_txt._x;

The code we’ve looked ...

Get Actionscript Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.