For those of you who have been developing in Flash and ActionScript for some time, the announcement of ActionScript 2.0 may well have struck you in many different ways. Perhaps there is some excitement about the new frontiers and the new possibilities that ActionScript 2.0 may provide. After all, it’s 2.0, that must represent improvements and new developments, right? On the other hand, the prospect of having to learn new syntax and new features may well send you retreating into a corner or trying to stick your head in the sand. In this article I’d like to foster your excitement and dispel your fears about ActionScript 2.0.
To start, let’s get the terminology straight. The ActionScript that you’ve been writing in Flash 5 and Flash MX is now referred to as ActionScript 1.0. In other words:
trace(“ActionScript 1.0” == “Flash 5/MX ActionScript”); // Displays: true
ActionScript 2.0 is a new feature included in both Flash MX 2004 and Flash MX Professional 2004, and in most respects it is very much the same language as ActionScript 1.0. That means that the majority of what you have learned with ActionScript 1.0 still applies to ActionScript 2.0. (I can hear a few sighs of relief already.) In fact, unless you’ve been writing custom classes in ActionScript, the differences between ActionScript 1.0 and ActionScript 2.0 are rather negligible. And, for those of you die-hard ActionScript 1.0 fans, you still have the option of developing applications using ActionScript 1.0. (Be aware, however, that mixing ActionScript 1.0 and ActionScript 2.0 code may or may not work.)
So what are the major features in ActionScript 2.0 that differentiate it from ActionScript 1.0? Glad you asked.
* Strong typing. This means that you can declare variables such that they can hold only one type of data. For example, you can declare a variable that is a MovieClip type, a String type, or a Color type. This also applies to function parameters. Strong typing helps to ensure that you are using good coding practices, and it helps to eliminate many mistakes that can arise from the loosely typed variables of ActionScript 1.0.
* Function return typing. This is a feature similar to strong typing. If a function returns a value then you can specify the type of value that it should return. If the function is not intended to return a value then you can specify that the function does not return a value. This is yet another feature that helps avoid many coding mistakes, and it works hand in hand with strong typing. For example, if you declare a function to return a String, and you try to assign that value to a variable typed as Number, then Flash will generate an error.
* Formal class syntax. This is, perhaps, the biggest new feature of ActionScript 2.0. ActionScript 1.0 lacks a formal class syntax, and instead, uses prototypes. ActionScript 2.0 supports public, private, and static class members, inheritance, and interfaces.
Those are some of the primary features of ActionScript 2.0. If you’re still reading, then let’s take a closer look at some of these points. In part one of this article we’ll look at strong typing and function return typing. The new formal class syntax is a big topic, and we’ll take a look at that in part two.
Strong Typing
ActionScript developers have long been spoiled with lose typing. That means that in ActionScript 1.0 you can declare a variable, assign a string value, and then assign a number value to the same variable:
var sProduct = “Flash MX 2004”;
sProduct = 6;
Of course, when you look at a simplified example like the preceding one, it is clear that a variable should only ever have one type of value. The variable sProduct should always contain a string value such as “Flash MX 2004” or “Dreamweaver MX 2004”, but it should not have a number value such as 6. This just makes sense. After all, in your application you want to be able to rely that the variable, when accessed at any point, will return a type of value that is useable. For example, if you want to display the value of sProduct to the user then it is important that you can rely on it being a string value and not an object of some kind. Make sense? Good.
ActionScript 1.0 tends to lend itself to poor programming practices…and to mistakes of assigning incorrect data types to variables. Many hours have been spent debugging applications just to isolate a line in which the wrong type of data was assigned. Strong typing is a compile-time (meaning it does not verify data types at runtime) feature of ActionScript 2.0 in which you can declare a variable to be of a particular data type. If you accidentally attempt to assign the wrong type of data to that variable, then when you use the check syntax feature or when you try to export/publish the application, Flash will give you an error message indicating where there is a type mismatch in your code. In order to declare a variable with strong typing, you must use the var keyword first, followed by the variable name, a colon, and the data type. All data type names are capitalized. Here is an example in which we declare sProduct as String:
var sProduct:String = “Flash MX 2004”;
After sProduct has been declared as String, if you attempt to assign a number value to it, Flash will give you an error. For example, if you attempt this:
sProduct = 6;
Then you will see the following error in the Output panel:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Type mismatch in assignment statement: found Number where String is required.
sProduct = 6;
Total ActionScript Errors: 1 Reported Errors: 1
Be sure to only use the var keyword when you are first declaring the variable. If you use the var keyword subsequently, then Flash will think you are declaring a new variable. It will create a new variable with the same name, overwriting the original. For example:
var sProduct = 6;
If you use the preceding code then Flash will create a new variable named sProduct. The new variable will not have any strong typing. The strong typing from the original variable will not be checked against the new variable in such a case and you will not receive any errors.