Carbon Dating ActionScript
There are thousands of ActionScript code samples online. Thousands. Google will conjure them from the ether within seconds, and any one of them might just suit your needs. But there’s often a problem, one I’ve seen knock the wind out of a developer’s sails. The problem is, any particular code sample may be outdated. That doesn’t necessarily mean it will fail, but then again.…
See, when Flash 5 was new, a number of scripting improvements in the platform led to a profusion of exciting new code samples online. No one at the time knew there would someday be an ActionScript 2.0, so no one bothered to label their samples as “ActionScript 1.0,” which makes perfect sense: the language wasn’t yet called ActionScript 1.0 — it was just ActionScript.
Coding has changed with each new release of Flash, and sometimes the changes necessitate modifications to the way ActionScript is written. I’ve seen dozens of posts to the Adobe forums where someone claims that a provided snippet “doesn’t work,” when in fact, it may simply be that the sample, written in ActionScript 2.0, has been used in a FLA published for ActionScript 1.0 — or the developer is still working with Flash MX, which doesn’t even have ActionScript 2.0.
So. Is there a way to tell, at a glance, how old a given sample of ActionScript is? Often enough, there is. Maybe not with 100% accuracy — but each version of ActionScript features a handful of tendencies that can be useful in making an educated guess.
ActionScript 3.0
Ah, the newest of the new. When Flash 9 arrives some time next year (assuming all goes well), it will be the first commercial release of Flash to support ActionScript 3.0. In the mean time, Flex Builder 2 is already on the market, and ActionScript 3.0 is definitely already alive and kicking.
At a glance, ActionScript 3.0 and 2.0 are kissin’ cousins. At the Flashforward 2006 conference in Austin, recently, I heard someone describe AS3 as “evolutionary, not revolutionary.” Cute, sure, but the description is apt. The language has matured, and the API is considerably enlarged, but in spite of numerous changes, including a completely overhauled event handling model, advanced users of AS2 shouldn’t have too many headaches making the transition.
Even so, here’s a list of AS3 stand-outs — not exhaustive, by any means — that might nudge you into thinking, “Hey, this may be AS3” before you get slap-happy pasting ActionScript 3.0 into a Flash 8 (or lower) timeline or class file.
- An abundance of packages and/or
importstatements. AS3 is heavy on organizing classes into packages. We saw the beginnings of this in AS2 with some of the more complex classes new to Flash 8, such asBitmapData, which actually resides in the packageflash.display.BitmapData. In AS3, just about everything is filed away in a package, and they all need to be imported. - The package statement. AS2 classes simply specify
class { code here }, while AS3 classes specifypackage { class { code here }}. - Lowercase datatypes. In AS3, there are a few new datatypes, such as
intanduint, that are named with all lowercase letters. AS2’s datatypes, such asNumber, always begin with a capital letter. Note that AS3 also hasNumber(and all the other initial cap datatypes), but be aware thatVoidis now the all-lowercasevoid. - An abundance of
addEventListenerevent handling. In AS3, you will no longer see code likemyButton.onRelease = function() {}; its event handling all looks like the listener object syntax used with the v2 Components. - Function/method parameters with the assignment operator. In AS3, if you write a function called
bringMe(food:String):void {}, whosefoodparameter specifies exactly what food to bring, that parameter must be set with the assignment operator if it is optional (e.g. …bringMe(food:String = "Turkish delight"):void {}) — you simply won’t see that in earlier versions of the language.
For additional details, check out the article “ActionScript 3.0 Overview,” by Gary Grossman and Emmy Huang.
ActionScript 2.0
Here are a couple hallmarks of AS2 code:
- Post colon suffixes. You’ve seen ’em. These are the
:Number,:String,:Object, etc., endings that appear in variable declarations and function/method signatures. Sure, AS3 uses the same syntax, but at least you’ll know to drop these suffixes if you’re porting something to AS1. Sometimes, that’s all you have to do to make an AS2 snippet work in the previous incarnation. Both AS1 and AS2, after all, compile to the same bytecode. Note: ActionScript 2.0 doesn’t require the strong datatyping provided by post colon suffixes. Even if you don’t see post colon syntax, ou might very well be looking at (lazily written) AS2. - The
importstatement. AS2 is the first to support bona fide class files, so you won’t seeimportin AS1. - The
classstatement. In AS2, classes are written as separate text files with the .as extension. These are defined by theclassstatement at the top of each .as file.
ActionScript 1.0
It’s important to realize that AS1 and AS2 are, under the hood, ultimately the same language. Generally speaking, if something can be done in AS2, it can be often be ported to AS1, but certain syntactical structure may have to be tweaked. Unless you’re dealing specifically with ActionScript classes (the code that specifies an object), it may be enough to drop the post colon suffixes on variables.
I can think of only a single “give away” that you’re dealing with AS1 code:
- An abundance of the
prototypeproperty. Before AS2 introduced the class statement, object-oriented programming (OOP) was approximated by use of theObject.prototypeproperty. For details, see this terrific (but incomplete) online book, Building Object-Oriented Applications in Flash 5. Only the first chapter appears to be complete (and there’s a bit of the second), but even so, this information is very useful for AS1 development — specifically object-oriented AS1. (It also happens to be the same approach used for OOP in JavaScript.)
Flash 4 ActionScript
This is the really old stuff — ancient stuff. Before Flash 5, ActionScript was a remarkably different animal. Flash 4 code can be difficult to read if you haven’t seen a lot of it, so keep the following characteristics in mind as you scan the web for code samples. Generally speaking, ActionScript of this vintage requires careful attention before it “works” in a modern setting. Flash 4 code samples should be taken more for the ideas they suggest than for their actual syntax.
- The
tellTarget()function. Deprecated since Flash 5 in favor of dot (.) notation and thewithstatement. - The
call()function. Deprecated since Flash 5 in favor of thefunctionstatement. - The
ifFrameLoaded()function. Deprecated since Flash five in favor of theMovieClip._framesloadedproperty. - Operators comprised of letters. Modern operators tend to be punctuation marks, such as
+(addition / string concatenation),>(greater than),==(equality),=(assignment), and the like. In the old days, many operators — mainly string operators — were made of letters, such asadd(string concatenation),gt(greater than),eq(quality), and so on. They stick out like sore thumbs, so they’re easy to spot. - Slash syntax. In Flash 3 and 4, the slash character (
/) was used to indicate the target path of a movie clip or variable. This was replaced in Flash 5 by the much more familiar (in terms of other languages) dot notation. See “Using slash syntax” in the section of Learning ActionScript 2.0 in Flash for details.
Keep in mind …
The above lists only represent the first few things that popped into my head. It’s far more important to keep in mind the general principle that ActionScript, like any other language, is a living thing. If you happen to copy/paste a quick snippet into your project and find that it a) doesn’t do what you expect or b) dumps hatfuls of error messages into the Output panel, pop open the ActionScript 2.0 Language Reference first before you stop, drop, and roll.
Search the term “Deprecated ActionScript” in the online help, or check it out on the LiveDocs site (make sure to click the Show Navigation button in the upper left if you do). You might very well be able to salvage what you’ve found, but you’ll have to get your bearings first.
October 21st, 2006 at 12:39 pm
Very useful!