Table C-1 summarizes key backward-compatibility issues and differences between ActionScript in Flash 4 versus Flash 5 and Flash 6.
Topic |
Description |
---|---|
Creating variables |
Flash 4's set function has been replaced by the var statement. |
Variable and timeline references |
The Flash 4-style slash-colon constructions (/square:area) have been superceded by dot notation (square.area). |
String comparison operators |
The Flash 4 string comparison operators—eq, ne, ge, gt, le, lt—have been superceded by the following operators in Flash 5 and later: = =, !=, >=, >, <=, <. |
String concatenation operator |
When creating Flash 4 content in Flash 5 or later, use the add operator instead of Flash 4's & operator. When authoring for Flash 5 or later, use the + operator for string concatenation. |
String length |
Flash 4's length( ) function (e.g., length(myString)) has been superceded by the String.length property (e.g., myString.length). |
Substring extraction |
Flash 4's substring( ) function (e.g., substring(myString, 1, 3)) has been superceded by the substring( ), substr( ), and slice( ) methods. Note that substring( ) differs in Flash 4 and Flash 5. |
Character code point functions |
Flash 4's chr( ) and mbchr( ) functions (used to create a character from a code point) have been superceded by String.fromCharCode( ). Flash 4's ord( ) and mbord( ) functions (used to determine the code point of a character) have been superceded by String.charCodeAt( ). |
Datatype conversion |
When importing Flash 4 files, Flash 5 and Flash MX automatically insert the Number( ) function around any numeric data that is used as an operand of the following potentially ambiguous operators: +, = =, !=, <>, <, >, >=, <=. |
The ifFrameLoaded statement |
Flash 3's ifFrameLoaded statement is deprecated. Use the _totalframes and _framesloaded MovieClip properties to create preloading code. |
Infinite loops |
Flash 4 allowed a maximum of 200,000 loop iterations. Flash 5 and Flash 6 allow 15 seconds for loops, after which they warn users that the movie has stopped responding. See Section 8.6.3. |
Subroutines versus functions |
In Flash 4, a subroutine could be created by attaching a block of code to a frame with a label and executing it using the call( ) statement. In Flash 5 and later, functions replace Flash 4 subroutines. |
Clip events |
Flash 4 supported only button events (i.e., functions starting with on( )). For modern event-handling techniques, see Chapter 10. |
Capturing keystrokes |
In Flash 4, keyPress was the only means of capturing keystrokes. In Flash 5 and later, the Key object and the movie clip events onClipEvent(keyDown) and onClipEvent(keyUp) offer much greater control over keyboard interaction. |
Tell Target deprecated |
Flash 4's Tell Target action (used to control remote movie clips) is replaced by properties and methods accessed using dot notation and the with statement. See "Whither Tell Target" later in this appendix. |
Get Property deprecated |
Flash 4's Get Property command is no longer required for movie clip property access. Use the dot operator instead. See Chapter 13. |
int deprecated |
Flash 4's int( ) function (used to truncate floats to integers) has been superceded by Math.floor( ), Math.ceil( ), and Math.round( ). |
Random number generation |
Flash 4's random( ) function (used to generate a random number) has been superceded by Math.random( ). |
Toggle High Quality deprecated |
Flash 4's Toggle High Quality action (used to set the rendering quality of the player) has been superceded by the global _quality property. |
_highquality deprecated |
Flash 4's _highquality property has been superceded by the global _quality property. |
Math object support in Flash 4 |
The functions and properties of the Math object (e.g., Math.cos( ), Math.PI ) are not natively supported by Flash Player 4. The values, however, are approximated when a movie is exported in Flash 4 format. |
loadMovie versus loadMovieNum |
Flash 3's loadMovie( ) onto a numbered level is superceded by loadMovieNum( ) (which accepts an integer level argument) as of Flash 5. Flash 4's loadMovie( ) into a target movie clip is still available as loadMovie( ) in Flash 5 and later. |
Printing |
Flash 5 and later support native print functions, which are available in Flash 4.0.20.0 and later as a modified Get URL action only. |
Objects and classes not supported |
Flash 4 does not support any of Flash 5's or Flash 6's built-in objects and classes (with the exception of Math as noted earlier in this table). |
In Chapter 13, we discussed how to control clips using Flash 5 (and later) techniques. Here we consider the equivalent Flash 4 techniques.
Prior to Flash 5, we executed special Movie Clip Actions to control a movie clip. We said, "Tell the clip named eyes to play," using the following:
Begin Tell Target ("eyes") Play End Tell Target
But as of Flash 5, movie clips can be controlled more directly, through built-in methods. For example:
eyes.play();
Similarly, to access the built-in properties of a movie clip prior to Flash 5, we used explicit property-getting and property-setting commands, such as:
GetProperty ("ball", _width) Set Property ("ball", X Scale) = 90
As of Flash 5, we can retrieve and set a movie clip's properties using the dot operator, just as we access the properties of any object:
ball._width; ball._xscale = 90;
Prior to Flash 5, to access variables inside a movie clip, we used a colon to separate the clip name from the variable name:
Set Variable: "x" = myClip:myVariable
As of Flash 5, a variable in a movie clip is simply a property of that clip object, so we use the dot operator to set and retrieve variable values:
x = myClip.myVariable;
Finally, prior to Flash 5, we accessed nested levels of movie clips using a directory-tree metaphor of slashes and dots:
clipA/clipB/clipC clipC
Because movie clips are object-like data as of Flash 5, we can access one clip as a property of another clip. We therefore use the dot operator to access so-called nested clips, and we use a clip's reserved _parent property to refer to the clip that contains it:
clipA.clipB.clipC; _parent._parent._parent.clipC;
The Flash 4-style slash-colon constructions, such as /square:area, have been superceded by dot syntax, a much more convenient way to refer to variables and timelines, in Flash 5 and later. The old syntax is deprecated and no longer recommended. Table C-2 shows equivalencies between Flash 4 and Flash 5 and later syntax when addressing variables. See Table C-1 for other syntactical differences.
Flash 4 syntax |
Flash 5 and later syntax |
Refers to . . . |
---|---|---|
/ |
_root |
Movie's main timeline |
/:x |
_root.x |
Variable x on movie's main timeline |
/clip1:x |
_root.clip1.x |
Variable x in instance clip1 on movie's main timeline |
/clip1/clip2:x |
_root.clip1.clip2.x |
Variable x in instance clip2, within instance clip1, within the main movie timeline |
_parent |
Timeline upon which the current clip resides (one level up from current clip timeline[C]) |
|
:x |
_parent.x |
Variable x on timeline upon which the current clip resides (one level up from current clip timeline) |
:x |
_parent._parent.x |
Variable x on timeline that contains the clip that contains the current clip (two levels up from current clip timeline) |
clip1:x |
clip1.x |
Variable x in instance clip1, where clip1 resides on the current timeline |
clip1/clip2:x |
clip1.clip2.x |
Variable x in instance clip2, where clip2 resides within clip1, which in turn resides on current timeline |
_level1:x |
_level1.x |
Variable x on the main timeline of a movie loaded onto level 1 |
_level2:x |
_level2.x |
Variable x on the main timeline of a movie loaded onto level 2 |
[C] The "current clip timeline" is the timeline that contains the code with the variable reference.
Although functions are the preferred mechanism for producing portable code modules, Flash 5 and Flash 6 still support Flash 4-style subroutines. In Flash 4, a subroutine could be created by attaching a block of code to a frame with a label. Later, the subroutine could be executed remotely via the Call action. But in Flash 4, any variable declared in a subroutine was nonlocal and persisted for the lifetime of the timeline on which it was defined. In Flash 5 and later, you can create local variables in subroutines the same way we create them in functions—using the var statement. However, variables defined with var in a subroutine are created as local variables only when the subroutine is executed via the Call function. If the script on the subroutine frame is executed as a result of the playhead simply entering the frame, the var statement declares a normal timeline nonlocal variable. Regardless, the more modern functions and local function variables should be used instead of subroutines.
In Flash 4, the string operators and the numeric operators were completely distinct—one set of operators worked only with strings, and the other set worked only with numbers. For example, the string concatenation operator in Flash 4 was &, but the mathematical addition operator was +. Similarly, string comparisons were performed with the eq and ne operators, but numeric comparisons were accomplished via = and <>. Table C-3 lists the Flash 5 and later syntax for analogous Flash 4 operators.
Operation |
Flash 4 syntax |
Flash 5 and later syntax |
---|---|---|
String concatenation |
+ or add |
|
String equality |
= = |
|
String inequality |
!= |
|
String comparison |
>=, >, <=, < |
|
Numeric addition |
+ |
+ |
Numeric equality |
= |
= = |
Numeric inequality |
<> |
!= |
Numeric comparison |
>=, >, <=, < |
>=, >, <=, < |
Some Flash 5 and later operators can operate on both strings and numbers. For example, when the + operator is used with strings, it concatenates its operands to form a new string. But when it is used with numbers, the + operator adds its two operands mathematically. Similarly, in Flash 5 and later, the equality operator (= =) and inequality operator (!=) are used to compare strings, numbers, and other datatypes.
Because many Flash 5 and later operators work with multiple datatypes, but Flash 4 operators do not, an ambiguity arises when a Flash 4 file is imported into Flash 5 or later. Therefore, when importing Flash 4 files, Flash 5 and Flash MX automatically insert the Number( ) function around any numeric data that is used as an operand of the following potentially ambiguous operators (unless the operand is a numeric literal): +, = =, !=, <>, <, >, >=, and <=.
Flash 4 files converted to Flash 5 or Flash MX format will also have the string concatenation operator (&) changed to the new add operator. Table C-4 contains examples of Flash 4-to-Flash 5 operator translation.
Flash 4 syntax |
Flash 5 and later syntax |
---|---|
Loop While (count <= numRecords) |
while (Number(count)<= Number(numRecords)) |
If (x = 15) |
if(Number(x) = = 15) |
If (y <> 20) |
if(Number(y) != 20) |
Set Variable: "lastName" = "kavanagh" |
lastName = "kavanagh" |
Set Variable: "name" = "molly" & lastName |
name = "molly" add lastName |
Most ActionScript events were first introduced in Flash 5. If you're exporting to Flash 4 format, use the button event handlers only (only button events were supported in Flash 4), and test your work carefully in Flash Player 4. To determine whether an event is supported by a particular version of the Flash Player, see the appropriate event entry in the Language Reference.
The Flash 4 string concatenation operator (&) performs a different operation (i.e., bitwise AND) in Flash 5 and later. If you're exporting a Flash 4 .swf file from a newer version of Flash, you must use the add operator to concatenate strings. Note that add is supported only for backward compatibility; the + operator is preferred in Flash 5 and later.
If you're exporting a Flash 4 .swf file, you must use the older eq and ne operators for string equality (= =) and inequality (!=) comparisons. The = = and != operators are preferred in Flash 5 and later.
If you're exporting a Flash 4 .swf file, you must use the older gt, ge, lt, and le string comparison operators. The >, >=, <, and <= operators are the preferred equivalents in Flash 5 and later.
If you're exporting a Flash 4 .swf file, you must use the older length( ) function to determine the length of a string. The length( ) function is supported for backward compatibility, but the String.length property is preferred in Flash 5 and later. Here we use the Flash 4 length( ) function to display the number of characters in the word "obsolete":
trace (length("obsolete")); // Displays: 8
In Flash 5, the String.substr( ) method is the string-extraction function that most closely resembles Flash 4's substring( ) function, which also used a start index and a length to retrieve a substring. Flash 5's String.substring( ) function uses start and end indexes.
If you're exporting a Flash 4 .swf file, you must use the older Flash 4 character-creation functions, chr( ) and mbchr( ), to create characters from numeric codes. In Flash 5 and later, String.fromCharCode( ) is preferred.
If you're exporting a Flash 4 .swf file, you must use the older Flash 4 code point functions, ord( ) and mbord( ) to determine the code point of a character. In Flash 5 and later, String.charCodeAt( ) is preferred.
Because all the Flash 5 string operations and functions work with multibyte characters, there's no way in Flash 5 to force a single-byte operation as there was in Flash 4. For example, the fromCharCode( ) function is as close as things get in Flash 5 to chr( ). The same is true of mblength( ) and length, mbsubstring( ) and substr( ), and mbord( ) and charCodeAt( ).
In Flash 4, Tell Target was our main tool for referring to movie clips. Tell Target, bless its soul, was an unwieldy tool and is rendered obsolete by the much more elegant object model introduced in Flash 5. The Tell Target function has been deprecated (i.e., retired from recommended use). Although we can still use the tellTarget( ) function to code in a Flash 4 manner, tellTarget( ) may disappear in the future.
Consider the following code, which uses Tell Target to play an instance named closingSequence:
Begin Tell Target ("closingSequence") Play End Tell Target
As of Flash 5, we simply invoke the much more convenient and readable play( ) method on the closingSequence instance:
closingSequence.play();
Tell Target could also perform multiple operations on an instance within a code block, like so:
Begin Tell Target ("ball") (Set Property: ("ball", x Scale) = "5") Play End Tell Target
As of Flash 5, the with( ) statement, described in Chapter 6, is the preferred way to achieve similar results:
with (ball) { _xscale = 5; play(); }