1.4 ActionScript 1.0 and 2.0 in Flash Player 6 and 7

 <  Day Day Up  >  

ActionScript 1.0 is based on the ECMAScript 3 standard (as is JavaScript 1.5), whereas ActionScript 2.0 is based on the emerging ECMAScript 4 standard (as is the theoretical JavaScript 2.0). As we learned in the Preface, under "ActionScript 2.0 Versus ActionScript 1.0," this common heritage gives the two versions a strong family resemblance; they share the same syntax for most non-OOP features, such as loops , conditionals, and operators.

Although ActionScript 2.0 is now the preferred version of ActionScript, ActionScript 1.0 syntax continues to be fully supported by Flash Player 7 and is not deprecated. As we'll see shortly, you can author either ActionScript 1.0 or ActionScript 2.0 in Flash MX 2004 and Flash MX Professional 2004 (but you cannot author ActionScript 2.0 in Flash MX). With a few minor exceptions, noted throughout the text, ActionScript 2.0 code is also backward compatible with Flash Player 6. However, ActionScript 2.0 is not compatible with older versions such as Flash Player 5 or Flash Player 4.

If you're an ActionScript 1.0 programmer, you can think of ActionScript 2.0 as a syntactic fa §ade over ActionScript 1.0. That is, both ActionScript 2.0 and ActionScript 1.0 compile to the same .swf bytecode (with a few minor additions for ActionScript 2.0). To the Flash Player, at runtime, there's effectively no difference between ActionScript 1.0 and ActionScript 2.0 (barring the aforementioned minor additions). For example, once an ActionScript 2.0 class, such as Rectangle , is compiled to a .swf file, it exists as a Function object at runtime, just as an older ActionScript 1.0 function declaration used as a class constructor would. Similarly, at runtime, an ActionScript 2.0 Rectangle instance ( r ) is given a _ _proto_ _ property that refers to Rectangle.prototype , again making it look to the Flash Player just like its ActionScript 1.0 counterpart .

But for the most part, you don't need to worry about these behind-the-scenes compiler and runtime issues. If you're moving to ActionScript 2.0 (and I think you should!), you can permanently forget ActionScript 1.0's prototype-based programming. In fact, most ActionScript 1.0 techniques for dynamically manipulating objects and classes at runtime are considered bad practice in ActionScript 2.0, and will actually lead to compiler errors when mixed with ActionScript 2.0 code. But never fear, this book highlights problematic ActionScript 1.0 practices and show you how to replace them with their modern ActionScript 2.0 counterparts.

1.4.1 Setting a Movie's ActionScript Version and Player Version

Flash MX 2004 lets you export .swf files (a.k.a. movies) in a format compatible with specific versions of the Flash Player. Don't confuse this with the version of the Flash Player the end user has installed (which is beyond your control except for checking their Player version and suggesting they upgrade when appropriate).

To set the version of a .swf file, use the drop-down list under File Publish Settings > Flash Version. For maximum compatibility, always set your .swf file's Flash Player version explicitly to the lowest version required, and no higher. If the .swf file version is higher than the end user's version of the Flash Player, it might not display correctly, and most code execution will fail.

Setting the version of a Flash movie has the following effects:

  • The movie will be compatible with (i.e., playable in) the specified version of the Flash Player (or later versions). In earlier versions, most ActionScript code will either not execute properly or not execute at all.

  • The movie will play properly in the most recent version of the Flash Player, even if it uses features that have changed since the specified version was released. In other words, the newest Flash Player will always play older format .swf files properly. For example, ActionScript identifiers in a Flash Player 6-format .swf file playing in Flash Player 7 are not case sensitive, even though identifiers in Flash Player 7-format .swf files are case sensitive. However, there's one exception: the security changes to the rules of cross-domain data loading in Flash Player 7 affect Flash Player 6-format .swf files in some cases. For details see http:// moock .org/asdg/technotes/crossDomainPolicyFiles.

When exporting Flash Player 6- and Flash Player 7-format movies from either Flash MX 2004 or Flash MX Professional 2004, you can tell Flash whether to compile your code as if it is ActionScript 1.0 or ActionScript 2.0. Naturally, you should make this choice at the beginning of development, as you don't want to rewrite your code at the end. To specify which version of the ActionScript compiler to use when creating a .swf file, use the drop-down list under File Publish Settings Flash ActionScript Version.

Throughout the remainder of the text, this book assumes you are using ActionScript 2.0's compiler.


When the ActionScript version is set to ActionScript 1.0, the following changes take effect:

  • ActionScript 2.0 syntax is not recognized and ActionScript 2.0 features, such as type checking (including post- colon syntax) and error handling, can either cause compiler errors (for Flash Player 6-format movies) or simply fail silently (for Flash Player 7-format movies).

  • Flash 4-style "slash syntax" for variables is allowed (but this coding style is deprecated and not recommended).

  • Reserved words added in ActionScript 2.0 such as class , interface , and public can be used as identifiers (but this practice makes code difficult to update and is highly discouraged).

The following runtime features of ActionScript 2.0 will not work in .swf files exported to a Flash Player 6-format .swf file, no matter which version of the Flash Player is used:

  • Exception handling (see Chapter 10).

  • Case sensitivity. (Scripts exported in Flash Player 6-format .swf files are not case sensitive, even in Flash Player 7. But beware! ActionScript 1.0 code in a Flash Player 7-format .swf file is case sensitive when played in Flash Player 7. See Table 1-2.)

  • Type casting (see "Runtime Casting Support" in Chapter 3).

Table 1-2 outlines case sensitivity for various possible permutations of the .swf file version and the user's Flash Player version. Note that runtime case sensitivity is unrelated to the ActionScript compiler version chosen and is dependent only on the format of the exported .swf file and the Flash Player version. In other words, both ActionScript 1.0 and 2.0 are case sensitive when exported in Flash Player 7-format .swf files and played in Flash Player 7. In other cases, code is case insensitive subject to the exceptions cited in the footnotes to Table 1-2. Consult my book ActionScript for Flash MX: The Definitive Guide (O'Reilly) for a full discussion of case sensitivity and its implications in Flash Player 6.

Table 1-2. Runtime case sensitivity support by language, file format, and Flash Player version

Movie compiled as either ActionScript 1.0 or 2.0 and

Played in Flash Player 6

Played in Flash Player 7

Flash Player 6-format .swf file

Case insensitive [5]

Case-insensitive [5]

Flash Player 7-format .swf file

Not supported [6]

Case-sensitive


[5] Identifiers (i.e., variable and property names), function names , frame labels, and symbol export IDs are case insensitive in Flash Player 6-format .swf files. However, reserved words such as "if" are case sensitive, even in Flash Player 6.

[6] Flash Player 6 cannot play Flash Player 7-format .swf files.

1.4.2 Changes to ActionScript 1.0 in Flash Player 7

In a Flash Player 7-format .swf file running in Flash Player 7, some ActionScript 1.0 code behaves differently than it does in Flash Player 6. These changes bring Flash Player 7 closer to full ECMAScript 3 compliance. Specifically:

  • The value undefined converts to the number NaN when used in a numeric context and to the string "undefined" when used in a string context (in Flash Player 6, undefined converts to the number 0 and to the empty string, "").

  • Any nonempty string converts to the Boolean value true when used in a Boolean context (in Flash Player 6, a string converts to true only if it can be converted to a valid nonzero number; otherwise , it converts to false ).

  • Identifiers (function names, variable names, property names, etc.) are case sensitive. For example, the identifiers firstName and firstname refer to two different variables in Flash Player 7. In Flash Player 6, the identifiers would refer to a single variable. (However, as usual, frame labels and symbol linkage IDs are not case sensitive.)

The preceding changes affect you only when you are updating a Flash Player 6-format movie to a Flash Player 7-format movie in order to use a feature unique to Flash Player 7. That is, if you upgrade your movie, you must test and possibly modify your code to make sure that it operates the same in Flash Player 7 format as it did in Flash Player 6 format. If you do not need Flash Player 7 features in your movie, you can continue to export it to Flash Player 6 format and it will usually run in Flash Player 7 exactly as it did in Flash Player 6. This last point cannot be emphasized enough.

Macromedia goes to great lengths to ensure that movies exported in older versions of the .swf format, such as Flash Player 6 format, continue to operate unchanged even if played in a later Player, such as Flash Player 7. However, when you publish a movie in Flash Player 7 format, you must be mindful of the changes implemented since the previous version of the .swf format. That is, the changes needed in your ActionScript depend on the .swf file version, not the Flash Player version.


Of course, any newly created .swf files exported in Flash Player 7 format (and not just those upgraded from Flash Player 6-format .swf files) must obey the new conventions, so keep them in mind moving forward. Remember that these new conventions bring ActionScript in line with other languages such as JavaScript and Java, making it easier to port code to or from other languages.

1.4.3 Flash 4 Slash Syntax Is Not Supported in ActionScript 2.0

In Flash 4 and subsequent versions, variables could be referenced with so-called "slash syntax." For example, in Flash 4, the following code is a reference to the variable x on the movie clip ball :

 /ball:x 

That syntax generates the following error if you attempt to use it with the ActionScript 2.0 compiler, whether exporting in Flash Player 6 or Flash Player 7 format:

 Unexpected '/' encountered 

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net