A useful Flash Class

Something that is really missing from Flash is any sort of layout control. Most Flash pieces tend to be a fixed size with fixed ui elements, so an automatic layout is not always missed. However, if you are making something like a gallery, a list control, or a dynamic form or interface, then flash developers typically revert to seting the x and y positions by hand. This is a common task that lends itself to some reusable code! To that end, I wrote a class that will automatically lay out a series of Display objects in a grid. It even resizes rows or columns based on the largest item or resizes items to fit. Plus it can align your objects no matter how they are centered by using it’s DisplayObject.getBounds() method. You just create the object, add some children to it, set the number of rows or columns (not both – the idea is that you can add as many objects as you want and it will expand to accomodate them) and call the updateGrid() method to set the positions. Here’s an example:

import com.archer.*;

var lgrid = new LayoutGrid();
lgrid.cols = 3;

// add some objects you want arranged
lgrid.addChild(someobject);
lgrid.addChild(someobject);
...

// calling this causes the layout grid
// to arrange all it's children
lgrid.updateGrid();

// tada, that's it.

Here’s a little Example. Click rearrange to randomly change the grid settings.

You can also set the maximum and minimum size of the cells. LayoutGrid will resize your child display objects to meet the maximum or minimum size.

This is the first release. There are a lot of features that could be added to make this class more robust.

Download it: LayoutGridAS3.zip

Leave a Reply

Close Modal

Contact Archer

Close Modal

We know you're still using an older version of Internet Explorer. Do you know how much mind-blowing stuff you're missing because of this?

Save yourself. Upgrade now.
We'll help you...