Though principally a review of a new O’Reilly book, this article will also talk about the capabilities of FlashMX and the Actionscript language.
Actionscript is the scripting language of Flash which many know as being responsible for annoying animated banner ads; it has also been popular for developing games. But since FlashMX it has many features that make it well-suited to many business applications. In particular for a front-end that must work from a web browser but requires interactivity, Flash is a much better solution than struggling with HTML and javascript, or even worse, HTML that calls a server-side script to generate a new HTML page when the user clicks anything.
Actionscript is very similar to Javascript: in fact many of the recipes from Javascript & DHTML Cookbook (that I reviewed a few months back) can be applied directly in your Flash movies. If all you’ve ever used Javascript for is mouseovers, or verifying form input, you may be surprised to discover just how powerful and interesting a language it is. You can build classes with constructors and even inheritance is supported.
It is an interpreted language and very weakly typed: if you call a function that does not exist it just does nothing, if you refer to a variable that has not been defined it treats it as zero, blank string or false depending on the context (you can ask if a variable has been defined or not). This weak typing, along with a complete lack of error reporting is the hardest thing about developing in Actionscript: there is no equivalent of PHP’s error_reporting(E_ALL) to catch your typos, and you have to resort to trace() statements to track down why things do not work as expected.
The syntax for class definitions takes a little getting used to: first define a constructor function just as with any normal function. Then to add variables and functions you assign them to a prototype. Here is an example:
//Define the class
function myclass(a){
this.a=a;
}
myclass.prototype.some_var=77;
myclass.prototype.some_action=function(b){
trace("a="+this.a+",some_var="+this.some_var+",b="+b);
}
//Use as follows
var v1=new myclass(10);
myclass.prototype.some_var=55; //Override class default
var v2=new myclass(); //Constructor parameter omitted
v1.some_action('xyz'); //Outputs "a=10,some_var=55,b=xyz"
v2.some_action('abc'); //Outputs "a=,some_var=55,b=abc"
FlashMX (also known as Flash 6) has some easy to use built-in methods for data communication with servers. You cannot directly connect to an SQL server, but instead use an intermediate script in a server-side language to take the data from the SQL server or server file system, perhaps modify or format it, then send it to the Flash client. This can be done with text strings, XML sockets, using FlashCom (mainly used for streaming video) or Flash Remoting.
Flash Remoting is a streamlined version of web services: the data is passed around in a special binary format instead of XML. Macromedia sell Flash Remoting servers for Java, Cold Fusion and .NET, but the format has been reverse-engineered and an open source version for PHP created (See amfphp.sf.net), and that has then been ported to PERL and Java. There are two distinct things you can do with Flash Remoting. First you can easily pass your back-end objects to Flash and have them created as Flash objects, and have all the data encoding and construction handled transparently. The second thing is you can access web services on other machines—the Flash Remoting server is referred to as a gateway for this reason. That means no need to understand SOAP, and no need to do any parsing. The data arrives ready to use.
Naturally Actionscript also has drawing functions, fine control over colour (including transparency and gradients) and text strings, and—important for us in Japan—uses Unicode internally. There are also form components, and javascript gives it many date and maths functions. In addition you can display jpegs, and play mp3s and videos, and it works equally well across Windows, Mac and Linux (at the time of writing Flash is the only software I have on my Linux workstation that will play video clips—I just cannot get the alternatives to work reliably).
Having teased you with the sexy features and got you all hot and excited, now comes the slap in the face: the Flash authoring software costs $500. Flash MX 2004 has just been released and seems to have split off the video options and some other things into a professional version at $700. There is a 30 day trial version available.
All is not lost: there are some cheaper commercial alternatives that produce Flash files, but their focus seems more on the drawing and animation side, and I do not know if you can use Actionscript. There is also an open source project called Ming, allowing a programmer to construct Flash files. It is fairly low level but does seem able to embed Actionscript. I do not know which of the data connectivity features of FlashMX are supported.
The Actionscript Cookbook follows the same format as other O’Reilly cookbooks: solutions to how-to questions. This is my favourite style of computer book, though here the original idea seems to have been lost and the author covers topics about which he has something to say, rather than it being a collection of the most common questions that developers ask. It is a good cover-to-cover read, but less good for picking at.
The book is comprehensive: part 1 covers local recipes, and part 2 covers remote recipes—connecting to the back-end. Then part 3 shows seven complete applications: a Paint program, an animation (very good as it shows the iterations development goes through), video chat, slideshow, mp3 jukebox, a web services “My Page” application, and a scheduler program.
The biggest thing missing in this book is screenshots: there are practically none, and for a book dealing with such a visual subject that is unacceptable. I understand that would have increased the book size, but I’d rather have lost some of the items and example applications if it meant I could see what the example code actually produces. Of course other computer books err too far the other way: I do not want to see a screenshot of a circle in the item on drawing circles. But at a minimum I want to see what each of the complete applications looks like when finished.
Other complaints? There is no attention paid to optimization, not even discussion of what might make your Flash movies slower or bigger. Maybe this is outside the scope but in a cookbook I would prefer to see high-quality professional solutions I can copy and paste directly into my code, rather than easy-to-understand solutions that I will end up having to rewrite if I discover they are too slow.
I would have liked to see more cross-referencing between items. In particular something that confused me when I first started with Flash is that if you want to fade text you have to embed the fonts. This is mentioned in item 8.20 on embedding fonts, but not in item 3.6 on fading movie clips.
In summary this book is not perfect, and it is not really a cookbook, but if you are doing anything with Actionscript then I think it is essential reading. It arrived in the middle of an Actionscript project and it has become the first book I reach for now. Recommended.


Actionscript Cookbook by Joey Lott.


