Back to Entry Page

Weaknesses in ActionScript

ActionScript has improved much since it first appeared in Flash 3, but the scripting language still leaves a few things to be desired. I'll be pointing out a few of these problems as we go.

Here are two points for starters, the first actually quite benign, the second more worrisome.

1. Placement of functions

JavaScript and its mightier cousins (C++, Java, PERL) let you define functions pretty much wherever you want them, so long as you set up a proper function body. While ActionScript does maintain the standard rules for function bodies, it differs from other contemporary languages in one key respect: In ActionScript, functions must appear within handlers.

In JavaScript the rule is exactly the reverse -- putting a function within a handler will cause confusion, if not havoc.

It's easy enough to live with this quirk, especially if you're not simultaneously building projects in both ActionScript and JavaScript: When dealing with Flash, you just make sure there's a convenient handler to house all your functions. General functions that may be invoked by several Movie Clips can be parked on a dummy Movie Clip buried in some non-visible layer. This clip's onClipEvent(load) handler provides a good niche.

2. No names involving variables

This problem is thornier. In JavaScript it's often very convenient to use a variable to point to one of several files, especially when you're trying to randomize content selection. For instance:

randy = Math.floor(Math.random()*3);
document.images[1].src = ("nose" + randy + ".jpg");

This little script lets us choose one of three noses, perhaps for a Mr. Virtual Potato Head. But try implementing this approach in ActionScript (using, say, loadMovie() instead of image reassignment) and you'll face a rude result. ActionScript won't allow you to use a variable within a filename.

To achieve the same result in ActionScript, you need a sequence of if tests, thus:

if(randy == 0) loadMovie("nose1.swf",0);
if(randy == 1) loadMovie("nose2.swf",0);
if(randy == 2) loadMovie("nose3.swf",0);

If this strikes you as annoying, join the club. We can only hope that future versions of Flash include improvements to this and other flaws in ActionScript.

Speaking of those other flaws, see this week's remarks about ActionScript and mouse clicks.


University of Baltimore Logo

Copyright © 2002 School of Information Arts and Technologies