Accessibility
 
Home / Developer Center / Macromedia Flash Developer Center /

Macromedia Flash Article

Branden Hall
Branden Hall
Senior Developer
WaxPraxis
 

Macromedia Flash MX undercover


Every time a new version of Macromedia Flash is released, there are a number of new features that don't get a lot of press, but make a huge difference in the day-to-day work of developers. Macromedia Flash MX is, in fact, chock-full of such features. In this first Flash MAX article I'd like to share the coolest new developer-centric features that you might not know about.

Happy returns
First of all, I use the attachMovie method of the movieclip object a lot. It was definitely one of my favorite features of Macromedia Flash 5. Often when I use attachMovie, it's within a loop. I'll create a movieclip dynamically, then make a "shortcut" to it so that I don't have to keep evaluating it by name. That shortcut can help save a lot of CPU cycles since evaluating a string is a relatively expensive operation. The code looks like this:

this.attachMovie("someClip", "foo"+i, i);
temp = this["foo"+i];

It was to my immense amusement and surprise when I was told that despite what the documentation says, attachMovie now returns a reference to the movieclip it just created. So now, with Macromedia Flash MX, the code can look like this:

temp = this.attachMovie("someClip", "foo"+i, i);

In fact, two other methods of the movieclip object, duplicateMovieClip and createNewMovieClip, also return references to the clips they create.

Data du jour
If you take a look at the methods available inside of the combo and list box components included with Macromedia Flash MX, you'll see that they have a curious method called setDataProvider. The actual documentation for this method recommends that users just pass an array to the method, which will be used to fill the component. However, the documentation also mentions an ActionScript object called DataProviderClass. The DataProviderClass is located in the asset folder of the component in the Library at:

/Flash UI Components/Core Assets - Developer Only/FUIComponent Class Tree/DataProvider

If you bravely wander that deep into the "developer only" parts of the components you'll find that the DataProviderClass isn't actually all that complicated. It's essentially a high level version of an array. The methods to really care about are:

addItem
addItemAt
removeItemAt
removeAll
replaceItemAt
getLength
getItemAt

All of these methods are pretty easy to understand once you look at the code. This high level way of shuffling around data isn't the really interesting part though. If you create an instance of DataProviderClass, and wire it up to a combo or list box you will have a live data connection between the data provider and the component. That is, anytime you update your instance of DataProviderClass, the component will update automatically. Assuming that combo_cb is your combo box, the code would look like this do to that "wiring":

dp = new DataProviderClass();
combo_cb.setDataProvider(dp);

Even cooler is the fact that multiple components can all be tied to one data provider and all updated anytime the data provider is modified. This becomes particularly interesting when you notice that a number of the components inside of the "Flash UI Components 2" and "Charting Components" libraries (which are available from the Macromedia Exchange for Flash) also support data providers.

Economy of scale
One new object in Macromedia Flash MX that most folks haven't used much is the Stage object. Sure, you can get the size of the stage from this object, and that's very helpful, but there's so much more there!

Imagine if you will, that you could create a Flash movie in which the contents would automatically re-arrange according to how much "room" is available on the stage. So, if the user resizes their browser the flash movie inside will know of that fact and rearrange the contents of the Flash movie accordingly. No need to imagine, you can do that now!

All you need to do is to utilize two properties of the Stage object, scaleMode and align as well as properly set the publish settings of your movie. First of all, in the HTML tab of your publish settings, set the dimension of your movie to use percent rather than 'match movie' or 'pixels'. Also, make sure that your movie's scale option is set to 'Default (Show All)'. These two options will make your movie scale to the size of the browser window.

Now, somewhere in your movie add the following two lines of code:

Stage.scaleMode = "noScale";
Stage.align = "tl";

The first line will prevent the actual content of your movie from scaling when the browser is scaled. The second line will make sure that the content of your SWF is always aligned at the top left. The reference info on Stage.align gives all of the other possible options for it.
Finally, for your movie to "hear" when the stage is resized, your have to set up a listener for the Stage object. The documentation on Stage.addListener shows how to do this, but in a nutshell your code will look like this:

this.onResize = function()
{
  //resize code here
}
Stage.addListener(this);

Now, anytime the stage is resized the onResize function will be called. Inside of the function you can simply use Stage.width and Stage.height to find out the "actual" size of stage.

Mincing words
Finally (for this article only though, as there are dozens of other lesser-known features in Flash MX) I'd like to highlight an event that belongs to the new LoadVars object. LoadVars is used to--you guessed it--load data from a URL. The event in question, onData, is automatically called when a URL is loaded, but before the raw information is parsed into name/value pairs. In fact, that raw information from the URL loaded is passed to onData.

This is useful because sometimes you don't want name/value pairs, you just want the raw information from a URL. For example, say a text file contains information that a marketing person has to update, like some advertising text.

To get to the raw data, just overwrite a LoadVars object's onData event, like this:

info = new LoadVars();
info.onData = function(raw){
  trace(raw);
}
info.load("somefile.txt");

Now, the raw variable inside of your onData method contains the raw information from somefile.txt.

Conclusion
Macromedia Flash MX is a very complex and powerful tool, and as such there is a lot more to it than meets the eye or appears in marketing messages. Make sure you take some time to dig into the product and find the features that will make your development easier. There's almost always another new feature to discover and learn about.



About the author
Branden Hall (bhall@waxpraxis.org) is an independant contractor in the Washington D.C. area. He spends most of his time developing high-end Macromedia Flash solutions, consulting, teaching, and moderating his very popular, code-centric mailing list, Flashcoders. He has also co-authored numerous Macromedia Flash related books with his most recent being "Object-Oriented Programming with ActionScript" from New Riders Press.