Web DevCenter    
 Published on Web DevCenter (http://www.oreillynet.com/javascript/)
 See this if you're having trouble printing code examples

Colin Moock on Flash MX

by Richard Koman

Macromedia's introduction of its suite of MX applications this summer (Flash MX, ColdFusion MX, Dreamweaver MX, and Fireworks MX) is a shot across the bow of enterprise software developers who might have written off the company's offerings as lightweight tools for HTML coders and designers who want to play around with ColdFusion.

For years, ColdFusion developers and ActionScript aficionados have been telling us that you can do more with this combination than people attuned to the Java or Microsoft development platform thought. Now Macromedia intends to make the point more firmly with its MX package of applications that Macromedia's tools are suited for enterprise-level development of transactions, web services, and a more sophisticated form of user interface that it calls Rich Internet Applications.

Tools aside, Macromedia believes one of its competitive edges entering this market is the nearly ubiquitous distribution of the Flash Player on web clients -- not just browsers across most platforms, but on handhelds and even some mobile phone platforms. It believes there's a strong selling proposition to be made in comparison to Java or .NET when a Flash-based application can run as well on a Windows, Mac, Linux, or Palm platform. To put a stake in the ground while demonstrating the system to developers, it has published a blueprint application called Pet Market, designed to enter the fray between Sun's Java Pet Store blueprint application and the .NET Pet Shop. (For a detailed comparison of these two, see Dean Wampler's "Cat Fight in a Pet Store: J2EE vs. .NET" on ONJava.com, Nov. 28, 2001.)

To learn more about the MX platform, we talked with Colin Moock, author of O'Reilly's ActionScript: The Definitive Guide.

Flash Clients

To get a handle on Macromedia's new paradigm, think "Flash clients" rather than "Flash movies." Developers are now starting to think about writing not just the user interface but full-blown clients with Flash and ActionScript -- Flash's ECMAScript-compliant scripting language.

To say that Flash is a client development platform is to question the idea that web clients must be browsers. But of course web clients aren't always browsers; the Web services concept says that web clients can be programs -- as long as they speak XML. The conventional wisdom is that you convert XML to HTML for presentation to users. But why not format XML for display in Flash rather than in HTML? With Flash's ability to display a variety of media in a rich, integrated environment, with its ability to hold open HTTP connections to servers for real-time updating of data, and its ability to write typed data to a user's disk, Flash looks at least as good as a client environment as HTML.

Related Reading

ActionScript: The Definitive Guide
Mastering Flash Programming
By Colin Moock

But is Flash truly an alternative environment to standard web development? "I think for application style websites, probably," Moock says. "For something where it really is a benefit to have a rich front end -- a hotel booking application is probably a good example. You want to immediately flip between pictures of different classes of rooms, or have a calendar that shows availability without having to pop up another window with a little JavaScript calendar picker that tries to set the date back in the parent window and shows JavaScript errors in Netscape ... For a rich front-end client experience, people will definitely be considering Flash much more seriously with Flash MX and beyond."

A great example of this is the reservation site for The Broadmoor hotel, where you can select dates of your stay, select your room type, number of rooms, adults, and children, and enter credit card information all within the Flash application. Changes to your reservation are updated immediately in the reservation summary.

With the addition of video, Flash naturally becomes even more interesting to the entertainment industry. "A movie site is just a natural for a Flash environment," says Moock. "It's just a holistic thing -- you don't have to go off to the downloads page and leave the running video."

And of course ActionScript enables a movie site to offer much more than a simple trailer download. A site could interrupt playback of the trailer with a user survey or an interactive game.

"You could mask video if you want to. It could be a background element," Moock says. "It could play until it reaches its one-hundreth frame and stop and ask you for some input. You could also overlay interactivity. It's not quite as graceful as say a SMIL file that would tell you specific timecodes, but you could do that all in Flash, having clickable portions of a video just by overlaying buttons, that sort of thing."

Flash offers a single environment for a world in which so many different types of media are used. "It sure is nice to be able to incorporate all these different types of media into one file format," Moock says. "It's not an HTML page that's broken into text, server calls, images, pop-ups that have RealPlayer in them ... it's nice to be able to work in one world and create these self-contained applications. If there's a video piece in your product description, then great, just add it in."

Real-Time Applications

For real-time applications, Flash can open a two-way, raw data connection over TCP/IP with a server. This allows for data to be updated in real-time in the Flash client, without the awkwardness of having a browser reload every x seconds. The applications for this are obvious, such as a live stock ticker or sports updates. Imagine a Flash movie that checks your auctions on eBay and alerts you when you've been outbid or when the auction is nearing completion. "You could have done these things in Flash 5," Colin said. "The impediment there was, you had to write the server yourself. In Flash 6 [dubbed Flash MX], Macromedia is moving to a product that will do lots of real-time stuff including audio and video, which could be quite cool." In the meantime, Moock has written a server called Unity to free developers from the trouble of writing their own.

Shared Object

Another addition to Flash MX is the "shared object," which lets Flash authors write a file to the user's file system. Unlike a browser cookie, which is merely a string, shared objects retain their type, so a stored array is still an array when you read it back. Moock says this enables Flash developers to create desktop-style applications -- up to a point."

"You could build an MP3 player in Flash," Moock says. "It's sort of crossing into what Director can do, but it's still limited by how much access you have to the local file system. Even so, you can do quite a few interesting connected applications that have such a small footprint and such an easy dev environment. You can get way, way richer than VB would ever let you get because the authoring environment is based on animation and multimedia." Moock cites the example of a home planning tool that lets you layout rooms and walls. "That's the sort of thing that would be so painful to do in VB but it's easy to do in Flash.

Components and Movie Clips

Even for self-contained Flash development, MX offers some important breakthroughs for Flash authors. One of the big ones is the new Components palette -- ready-made UI elements that you can simply drag onto the stage, much like in VisualBasic. Components are possible because Macromedia fixed the separation between code and visual assets, which was an irritant in Flash 5. "While you had code-based objects and classes manipulating all your data, the problem was you had to associate code with a movie clip that actually flies around the screen," Moock explains. "The link between those two was always a bit of a pain, because movie clips themselves are represented by objects in ActionScript. You have object-oriented access to a movie clip but you have to maintain two hierarchies -- one of all the data in your movie and one of all your onscreen assets. So, a big change in 6 is that they let you hook movie clip classes into the regular class architecture. All you code can now go into one movieclip, and then you marry the two together.

"That's in fact what enables quite a lot of the components they're bringing out. Each of those is a movie clip class. If you imagine a VB environment, where you're bringing the listbox onto the screen and sizing it ... the way that's pulled off in Flash, each radio button or listbox that you're dragging onstage is an instance of a movie clip, and that movie clip is in fact a class that controls how that individual radio button operates. It was really difficult to build apps that are scalable and portable without the work they've done on improving the object model of movie clips. .... You need a rich API to control all these interface elements. The UI components that are built in really make that whole deal so much easier. There was a time when you would be building all your radio button classes by hand yourself, which might have been a month of the project."

Event Model

Event handling is another important area much improved in MX. The problem of "empty movie clip syndrome" -- where you created movie clips on the stage just to hold an event handler -- is a thing of the past. "You used to put event handlers directly onto objects at author time, so you would literally select your interface widget and you type into a text area, 'Here's my event handler.' So, you'd have all these event handlers sprinkled all over your movie," Moock explains. "Traditional developers really hated that sort of sloppiness. That was part of the quirkiness of Flash. They didn't understand, 'Why do I have to physically put something into this environment just so that I can capture a keystroke?'

"In MX you can assign event handlers remotely, using the normal JavaScript syntax. Now you can just say button.onPress equals this function."

ActionScript implements JavaScript's listener event model, so you don't need do click capturing on the stage at all. "Any object can be listening to a keypress by defining the keydown event and adding that object to the listener of the key or mouse object." Listeners can make the whole application more usable, Moock says. "In a lot of custom stuff, the developers don't bother adding tabbing order and accessibility and stuff like that."


While XML support is not new in MX, it (as well as string and array) was rewritten as a native C++ object. The result is that XML parsing is 50 to 500 times faster in Flash 6, and some tests are in the 800-to 1,000-times-faster range. You can now realistically load in, say, 300K of XML and expect it to be parsed without any problem. The faster XML also really helps multiuser apps. "Multiuser apps built in XML traditionally were pretty clunky because there was so much parsing going on -- 10 times a second it might be parsing out a 500 byte XML file, which adds up."

The Standards Problem

While Flash has certainly grown up, and may even become the preferred way of writing Internet clients, does Moock worry over the fact that so much content is going into a binary format controlled by a single corporation?

"I really used to have concerns about that. I come from a very standards-based SGML background. There are real problems. I had concerns even about accessibility," Moock says. Web browsers are way more accessible that Flash movies and even though Flash has made up some ground, it's still really not there. Until Flash 6, it didn't support international text, no unicode. I really love the Web because there's all this data up there and it's searchable and interchangeable and all those things the web is so great for.

"Sometime last year, I just decided the whole thing, even the existence of the Web itself, is an evolution. Basically Flash wouldn't be as popular as it is if it didn't fill some need and desire that the general population is creating. If we need to go through a single vendor's implementation of a platform that everybody wants, then we need to go through it. Eventually who knows, maybe it will die for the reasons everybody's worried about; on the other hand, maybe it won't. That really is just the natural development of the new medium.

"Flash is a loud message to everyone who is working on standards: this is obviously a need, so maybe it's time to pay attention to this kind of content. Maybe that's not possible in the context of a standards body. It's something that I've been emotional about on both sides and now I'm just agnostic because I'm interested to see how it develops."

Richard Koman is a freelancer writer and editor based in Sonoma County, California. He works on SiliconValleyWatcher, ZDNet blogs, and is a regular contributor to the O'Reilly Network.

Return to the Web Development DevCenter.

Copyright © 2009 O'Reilly Media, Inc.