Add Life to the Web, Afterburner, Aftershock, Andromedia, Allaire, Animation PowerPack, Aria, Attain, Authorware,
Authorware Star, Backstage, Bright Tiger, Clustercats, ColdFusion, Contribute, Design In Motion, Director, Dream Templates,
Dreamweaver, Drumbeat 2000, EDJE, EJIPT, Extreme 3D, Fireworks, Flash, Flash Lite, Flex, Fontographer, FreeHand,
Generator, HomeSite, JFusion, JRun, Kawa, Know Your Site, Knowledge Objects, Knowledge Stream, Knowledge Track,
LikeMinds, Lingo, Live Effects, MacRecorder Logo and Design, Macromedia, Macromedia Action!, Macromedia Breeze,
Macromedia Flash, Macromedia M Logo and Design, Macromedia Spectra, Macromedia xRes Logo and Design, MacroModel,
Made with Macromedia, Made with Macromedia Logo and Design, MAGIC Logo and Design, Mediamaker, Movie Critic,
Open Sesame!, Roundtrip, Roundtrip HTML, Shockwave, Sitespring, SoundEdit, Titlemaker, UltraDev, Web Design 101, what
the web can be, and Xtra are either registered trademarks or trademarks of Macromedia, Inc. and may be registered in the United
States or in other jurisdictions including internationally. Other product names, logos, designs, titles, words, or phrases mentioned
within this publication may be trademarks, service marks, or trade names of Macromedia, Inc. or other entities and may be
registered in certain jurisdictions including internationally.
Third-Party Information
This guide contains links to third-party websites that are not under the control of Macromedia, and Macromedia is not
responsible for the content on any linked site. If you access a third-party website mentioned in this guide, then you do so at your
own risk. Macromedia provides these links only as a convenience, and the inclusion of the link does not imply that Macromedia
endorses or accepts any responsibility for the content on those third-party sites.
Speech compression and decompression technology licensed from Nellymoser, Inc. (www.nellymoser.com).
Sorenson™ Spark™ video compression and decompression technology licensed from Sorenson Media, Inc.
APPLE COMPUTER, INC. MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, REGARDING THE
ENCLOSED COMPUTER SOFTWARE PACKAGE, ITS MERCHANTABILITY OR ITS FITNESS FOR ANY
PARTICULAR PURPOSE. THE EXCLUSION OF IMPLIED WARRANTIES IS NOT PERMITTED BY SOME STATES.
THE ABOVE EXCLUSION MAY NOT APPLY TO YOU. THIS WARRANTY PROVIDES YOU WITH SPECIFIC
LEGAL RIGHTS. THERE MAY BE OTHER RIGHTS THAT YOU MAY HAVE WHICH VARY FROM STATE TO
STATE.
This manual describes the syntax and use of ActionScript elements in Macromedia Flash
MX 2004 and Macromedia Flash MX Professional 2004. To use examples in a script, copy the
code example from this book and paste it into the Script pane or into an external script file. The
manual lists all ActionScript elements—operators, keywords, statements, actions, properties,
functions, classes, and methods.
Note: The examples are written in ActionScript 2.0, and will be unlikely to compile if you have
specified ActionScript 1.0 or Flash Player 5 or earlier in the Flash tab of your FLA file’s Publish
Settings dialog box.
There are two types of entries in this manual:
• Individual entries for operators, keywords, functions, variables, properties, methods,
and statements
• Class entries, which provide general information about built-in classes
Use the information in the sample entries to interpret the structure and conventions used in these
types of entries.
The following sample entry explains the conventions used for all ActionScript elements that are
not classes.
Entry title
Entries are listed alphabetically within a chapter. The alphabetization ignores capitalization,
leading underscores, and so on.
21
Availability
Unless otherwise noted, the Availability section tells which versions of Flash Player support the
element. The supported Flash Player version is not the same as the version of Flash used to author
the content. For example, if you use Macromedia Flash MX 2004 or Macromedia Flash MX
Professional 2004 to create content for Flash Player 6, you can use only ActionScript elements
that are available to Flash Player 6.
In a few cases, this section also indicates which version of the authoring tool supports an element.
For an example, see System.setClipboard().
If an element is supported only in ActionScript 2.0, that information is also noted in this section.
Usage
This section provides correct syntax for using the ActionScript element in your code. The
required portion of the syntax is in
information are in
italicized code font. Data types can be distinguished from code that you
provide by the colon (:) that always precedes data type information. Brackets (
code font. Both the code that you provide and data type
[]) indicate
optional parameters.
Parameters
This section describes any parameters listed in the syntax.
Returns
This section identifies what, if any, values the element returns.
Description
This section identifies the type of element (for example, operator, method, function, and so on)
and then describes how to use the element.
Example
This section provides a code sample demonstrating how to use the element.
See also
This section lists related ActionScript dictionary entries.
Sample entry for classes
The following sample dictionary entry explains the conventions used for built-in ActionScript
classes. .
Entry title
The entry title provides the name of the class. The class name is followed by general
descriptive information.
22Chapter 1: Introduction
Method and property summary tables
Each class entry contains a table listing all of the associated methods. If the class has properties
(often constants), event handlers, or event listeners, these elements are summarized in additional
tables. All the elements listed in these tables also have their own entries, which follow the class
entry.
Constructor
If a class requires that you use a constructor to access its methods and properties, the constructor
is described in each class entry. This description has all of the standard elements (usage,
description, and so on) of other entries.
Method, property, and event handler listings
The methods, properties, and event handlers of a class are listed alphabetically after the class
entry.
Examples Folder
A set of ActionScript example files can be found in the HelpExamples folder in the Flash
installation directory. Typical paths to this folder are:
The HelpExamples folder contains a set of FLA files that are complete projects with working
ActionScript code. All ActionScript code can be found in the main Timeline of each FLA file or
in external ActionScript (.as) files.
Examples Folder23
24Chapter 1: Introduction
CHAPTER 2
ActionScript Language Reference
–– (decrement)
Availability
Flash Player 4.
Usage
––expression
expression––
Parameters
expression
Returns
A number.
Description
Operator (arithmetic); a pre-decrement and post-decrement unary operator that subtracts 1 from
the
expression. The expression can be a variable, element in an array, or property of an object.
The pre-decrement form of the operator (
returns the result. The post-decrement form of the operator (
expression and returns the initial value of expression (the value prior to the subtraction). For
more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
A number or a variable that evaluates to a number.
––expression) subtracts 1 from expression and
expression––) subtracts 1 from the
Example
The pre-decrement form of the operator decrements x to 2 (x - 1 = 2) and returns the result as y:
var x:Number = 3;
var y:Number = --x;
//y is equal to 2
The post-decrement form of the operator decrements x to 2 (x - 1 = 2) and returns the original
value of
var x:Number = 3;
var y:Number = x--;
//y is equal to 3
x as the result y:
25
The following example loops from 10 to 1, and each iteration of the loop decreases the counter
variable
for (var i = 10; i>0; i--) {
}
i by 1.
trace(i);
26Chapter 2: ActionScript Language Reference
++ (increment)
Availability
Flash Player 4.
Usage
++expression
expression++
Parameters
expression
Returns
A number.
Description
Operator (arithmetic); a pre-increment and post-increment unary operator that adds 1 to
expression. The expression can be a variable, element in an array, or property of an object.
The pre-increment form of the operator (
result. The post-increment form of the operator (
returns the initial value of
The pre-increment form of the operator increments
as
y:
var x:Number = 1;
var y:Number = ++x;
trace("x:"+x);//traces x:2
trace("y:"+y);//traces y:2
The post-increment form of the operator increments x to 2 (x + 1 = 2) and returns the original
value of
var x:Number = 1;
var y:Number = x++;
trace("x:"+x);//traces x:2
trace("y:"+y);//traces y:1
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
A number or a variable that evaluates to a number.
++expression) adds 1 to expression and returns the
expression++) adds 1 to expression and
expression (the value prior to the addition).
x to 2 (x + 1 = 2) and returns the result
x as the result y:
Example
The following example uses ++ as a post-increment operator to make a while loop run five times:
var i:Number = 0;
while (i++<5) {
trace("this is execution "+i);
}
/* output:
this is execution 1
this is execution 2
this is execution 3
this is execution 4
++ (increment)27
this is execution 5
*/
The following example uses ++ as a pre-increment operator:
var a:Array = [];
// var a:Array = new Array();
var i:Number = 0;
while (i<10) {
Operator (inequality); tests for the exact opposite of the equality (==) operator. If expression1 is
equal to
expression2, the result is false. As with theequality (==) operator, the definition of
equal depends on the data types being compared, as illustrated in the following list:
• Numbers, strings, and Boolean values are compared by value.
• Objects, arrays, and functions are compared by reference.
• A variable is compared by value or by reference, depending on its type.
Comparison by value means what most people would expect equals to mean—that two
expressions have the same value. For example, the expression (2 + 3) is equal to the expression
(1 + 4) when compared by value.
Comparison by reference means that two expressions are equal only if they both refer to the same
object, array, or function. Values inside the object, array, or function are not compared.
When comparing by value, if
ActionScript will attempt to convert the data type of
expression1. For more information, see “Automatic data typing” and “Operator precedence and
expression1 and expression2 are different data types,
expression2 to match that of
associativity” in Using ActionScript in Flash.
Example
The following example illustrates the result of the inequality (!=) operator:
Operator; tests for the exact opposite of the strict equality (===) operator. The strict inequality
operator performs the same as the inequality operator except that data types are not converted.
For more information, see “Automatic data typing” in Using ActionScript in Flash. If
is equal to
equality (
expression2, and their data types are equal, the result is false. As with thestrict
===) operator, the definition of equal depends on the data types being compared, as
expression1
illustrated in the following list:
• Numbers, strings, and Boolean values are compared by value.
• Objects, arrays, and functions are compared by reference.
• A variable is compared by value or by reference, depending on its type.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The comments in the following code show the returned value of operations that use the equality
(
==), strict equality (===), and strict inequality (!==) operators:
var s1:String = "5";
var s2:String = "5";
var s3:String = "Hello";
var n:Number = 5;
var b:Boolean = true;
Flash Player 4. In Flash 4 files, the % operator is expanded in the SWF file as x - int(x/y) * y
and may not be as fast or as accurate in later versions of Flash Player.
Usage
expression1 % expression2
Parameters
None.
Returns
A number.
Description
Operator (arithmetic); calculates the remainder of expression1 divided by expression2. If
either of the
convert them to numbers. The
value. For more information, see “Automatic data typing” and “Operator precedence and
associativity” in Using ActionScript in Flash.
Example
The following numeric example uses the modulo (%) operator:
The first trace returns 2, rather than 12/5 or 2.4, because the modulo (%) operator returns only
the remainder. The second trace returns 0.0999999999999996 instead of the expected 0.1
because of the limitations of floating-point accuracy in binary computing.
expression parameters are non-numeric, the modulo (%) operator attempts to
expression can be a number or string that converts to a numeric
See Also
int, / (division).
34Chapter 2: ActionScript Language Reference
%= (modulo assignment)
Availability
Flash Player 4.
Usage
expression1 %= expression2
Parameters
None.
Returns
A number.
Description
Operator (arithmetic compound assignment); assigns expression1 the value of expression1%
expression2
x %= y
x = x % y
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example assigns the value 4 to the variable x:
var x:Number = 14;
var y:Number = 5;
trace(x%=y); // output: 4
. The following two expressions are equivalent:
See also
% (modulo)
%= (modulo assignment)35
& (bitwise AND)
Availability
Flash Player 5. In Flash 4, the AND (&) operator was used for concatenating strings. In Flash 5
and later, the AND (&) operator is a bitwise AND, and you must use the addition (
concatenate strings. Flash 4 files that use the AND (
&) operator are automatically updated to use
addition (+) operator when imported into the Flash 5 or later authoring environment.
Usage
expression1 & expression2
Parameters
None.
Returns
A 32-bit integer.
Description
Operator (bitwise); converts expression1 and expression2 to 32-bit unsigned integers, and
performs a Boolean AND operation on each bit of the integer parameters. Floating-point
numbers are converted to integers by discarding any digits after the decimal point. The result is a
new 32-bit integer.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value using the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and then have the most significant digits discarded as well.
The return value is interpreted as a two’s complement number with sign, so the return is an
integer in the range -2147483648 to 2147483647.
+) operator to
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example compares the bit representation of the numbers and returns 1 only if both
bits at the same position are 1. In this ActionScript, you add 13 (binary 1101) and 11 (binary
1011) and return 1 only in the position where both numbers have a 1.
var insert:Number = 13;
var update:Number = 11;
trace(insert & update);// output : 9 (or 1001 binary)
In the numbers 13 and 11 the result is 9 because only the first and last positions in both numbers
have the number 1.
The following examples show the behavior of the return value conversion:
Operator (logical); performs a Boolean operation on the values of one or both of the expressions.
Evaluates
expression evaluates to
on the right side of the operator) is evaluated. If
true; otherwise, it is false.
ExpressionEvaluates
true&&true
true&&false
false&&false
false&&true
expression1 (the expression on the left side of the operator) and returns false if the
false. If expression1 evaluates to true, expression2 (the expression
expression2 evaluates to true, the final result is
true
false
false
false
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example uses the logical AND (&&) operator to perform a test to determine if a
player has won the game. The
turns variable and the score variable are updated when a player
takes a turn or scores points during the game. The script shows “You Win the Game!” in the
Output panel when the player’s score reaches 75 or higher in 3 turns or less.
var turns:Number = 2;
var score:Number = 77;
if ((turns<=3) && (score>=75)) {
function The function to be performed on the contents of the parentheses.
parameter1...parameterN A series of parameters to execute before the results are passed as
parameters to the function outside the parentheses.
Returns
Nothing.
Description
Operator; performs a grouping operation on one or more parameters, performs sequential
evaluation of expressions, or surrounds one or more parameters and passes them as parameters to
a function outside the parentheses.
Usage 1: Controls the order in which the operators execute in the expression. Parentheses override
the normal precedence order and cause the expressions within the parentheses to be evaluated
first. When parentheses are nested, the contents of the innermost parentheses are evaluated before
the contents of the outer ones.
Usage 2: Evaluates a series of expressions, separated by commas, in sequence, and returns the
result of the final expression.
Usage 3: Surrounds one or more parameters and passes them as parameters to the function
outside the parentheses.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Numbers, strings, variables, or text.
Example
Usage 1: The following statements show the use of parentheses to control the order in which
expressions are executed (the value of each expression appears in the Output panel):
Usage 2: The following example evaluates the function foo(), and then the function bar(), and
returns the result of the expression
var a:Number = 1;
var b:Number = 2;
a + b:
() (parentheses)41
function foo() {
a += b;
}
function bar() {
b *= 10;
}
trace((foo(), bar(), a+b)); // outputs 23
Usage 3: The following example shows the use of parentheses with functions:
var today:Date = new Date();
trace(today.getFullYear()); // traces current year
function traceParameter(param):Void {
trace(param);
}
traceParameter(2*2);//traces 4
See also
with
42Chapter 2: ActionScript Language Reference
– (minus)
Availability
Flash Player 4.
Usage
(Negation) -expression
(Subtraction) expression1 - expression2
Parameters
None.
Returns
An integer or floating-point number.
Description
Operator (arithmetic); used for negating or subtracting.
Usage 1: When used for negating, it reverses the sign of the numerical
expression.
Usage 2: When used for subtracting, it performs an arithmetic subtraction on two numerical
expressions, subtracting
expression2 from expression1. When both expressions are integers,
the difference is an integer. When either or both expressions are floating-point numbers, the
difference is a floating-point number.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
Usage 1: The following statement reverses the sign of the expression 2 + 3:
trace(-(2+3));// output: -5
Usage 2: The following statement subtracts the integer 2 from the integer 5:
trace(5-2);// output: 3
The result, 3, is an integer.
Usage 3: The following statement subtracts the floating-point number 1.5 from the floating-point
number 3.25:
trace(3.25-1.5);// output: 1.75
The result, 1.75, is a floating-point number.
– (minus)43
* (multiplication)
Availability
Flash Player 4.
Usage
expression1 * expression2
Parameters
None.
Returns
An integer or floating-point number.
Description
Operator (arithmetic); multiplies two numerical expressions. If both expressions are integers, the
product is an integer. If either or both expressions are floating-point numbers, the product is a
floating-point number.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
Usage 1: The following statement multiplies the integers 2 and 3:
trace(2*3); // output: 6
The result, 6, is an integer.
Usage 2: This statement multiplies the floating-point numbers 2.0 and 3.1416:
trace(2.0 * 3.1416); // output: 6.2832
The result, 6.2832, is a floating-point number.
44Chapter 2: ActionScript Language Reference
*= (multiplication assignment)
Availability
Flash Player 4.
Usage
expression1 *= expression2
Parameters
None.
Returns
The value of expression1 * expression2. If an expression cannot be converted to a numeric
value, it returns
Description
Operator (arithmetic compound assignment); assigns expression1 the value of expression1 *
expression2. For example, the following two expressions are equivalent:
x *= y
x = x * y
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
Usage 1: The following example assigns the value 50 to the variable x:
var x:Number = 5;
var y:Number = 10;
trace(x *= y); // output: 50
Usage 2: The second and third lines of the following example calculate the expressions on the
right side of the equal sign and assign the results to
var i:Number = 5;
var x:Number = 4-6;
var y:Number = i+2;
trace(x *= y); // output: -14
NaN (not a number).
x and y:
See also
* (multiplication)
*= (multiplication assignment)45
, (comma)
Availability
Flash Player 4.
Usage
(expression1, expression2 [, expressionN...])
Parameters
None.
Returns
The value of expression1, expression2, and so on.
Description
Operator; evaluates expression1, then expression2, and so on. This operator is primarily used
with the
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example uses the comma (,) operator in a for loop:
for (i = 0, j = 0; i < 3 && j < 3; i++, j+=2) {
}
// Output:
// i = 0, j = 0
// i = 1, j = 2
The following example uses the comma (,) operator without the parentheses () operator and
illustrates that the comma operator returns only the value of the first expression without the
parentheses () operator:
for loop statement and is often used with the parentheses () operator.
trace("i = " + i + ", j = " + j);
var v:Number = 0;
v = 4, 5, 6;
trace(v); // output: 4
The following example uses the comma (,) operator with the parentheses () operator and
illustrates that the comma operator returns the value of the last expression when used with the
parentheses () operator:
var v:Number = 0;
v = (4, 5, 6);
trace(v); // output: 6
The following example uses the comma (,) operator without the parentheses () operator and
illustrates that the comma operator sequentially evaluates all of the expressions but returns the
value of the first expression. The second expression,
z++, is evaluated and z is incremented by
one.
var v:Number = 0;
var z:Number = 0;
46Chapter 2: ActionScript Language Reference
v = v + 4 , z++, v + 6;
trace(v); // output: 4
trace(z); // output: 1
The following example is identical to the previous example except for the addition of the
parentheses () operator and illustrates once again that, when used with the parentheses () operator,
the comma (,) operator returns the value of the last expression in the series:
var v:Number = 0;
var z:Number = 0;
v = (v + 4, z++, v + 6);
trace(v); // output: 6
trace(z); // output: 1
See also
() (parentheses)
, (comma)47
. (dot)
Availability
Flash Player 4.
Usage
object.property_or_method
instancename.variable
instancename.childinstance
instancename.childinstance.variable
Parameters
object
An instance of a class. The object can be an instance of any of the built-in ActionScript
classes or a custom class. This parameter is always to the left of the dot (.) operator.
property_or_method The name of a property or method associated with an object. All the
valid methods and properties for the built-in classes are listed in the method and property
summary tables for that class. This parameter is always to the right of the dot (.) operator.
instancename The instance name of a movie clip.
childinstance A movie clip instance that is a child of, or nested in, another movie clip.
variable A variable on the Timeline of the movie clip instance name to the left of the dot
(
.) operator.
Returns
The method, property or movie clip named on the right side of the dot.
Description
Operator; used to navigate movie clip hierarchies to access nested (child) movie clips, variables, or
properties. The dot operator is also used to test or set the properties of an object or top-level class,
execute a method of an object or top-level class, or create a data structure.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example identifies the current value of the variable hairColor in the movie clip
person_mc:
person_mc.hairColor
The Flash 4 authoring environment did not support dot syntax, but Flash MX 2004 files
published for Flash Player 4 can use the dot operator. The preceding example is equivalent to the
following (deprecated) Flash 4 syntax:
/person_mc:hairColor
The following example creates a new movie clip within the _root scope. Then a text field is
created inside the movie clip called
true and then populated with the current date.
48Chapter 2: ActionScript Language Reference
container_mc. The text field’s autoSize property is set to
0, 100, 22);
this.container_mc.date_txt.autoSize = true;
this.container_mc.date_txt.text = new Date();
The dot (.) operator is used when targeting instances within the SWF file and when you need to
set properties and values for those instances.
. (dot)49
: (type)
Availability
Flash Player 6.
Usage
[modifiers] var variableName:type
function functionName():type { ... }
function functionName(parameter1:type, ... , parameterN:type) [ :type ]{ ... }
Note: To use this operator, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash
tab of your FLA file’s Publish Settings dialog box.
Parameters
variableName
type A native data type, class name that you have defined, or interface name.
functionName An identifier for a function.
parameter An identifier for a function parameter.
Description
Operator; used for strict data typing; this operator specifies the variable type, function return
type, or function parameter type. When used in a variable declaration or assignment, this
operator specifies the variable’s type; when used in a function declaration or definition, this
operator specifies the function’s return type; when used with a function parameter in a function
definition, this operator specifies the variable type expected for that parameter.
Types are a compile-time-only feature. All types are checked at compile time, and errors are
generated when there is a mismatch. (For more information, see Appendix A, “Error Messages,”
in Using ActionScript in Flash.) Mismatches can occur during assignment operations, function
calls, and class member dereferencing using the dot (
use strict data typing (see “Strict data typing” in Using ActionScript in Flash).
Types that you can use include all native object types, classes and interfaces that you define, and
Function and Void. The recognized native types are Boolean, Number, and String. All built-in
classes are also supported as native types. For more information, see “About data types” in Using ActionScript in Flash.
For information about operator precedence, see “Operator precedence and associativity” in Using ActionScript in Flash.
An identifier for a variable.
.) operator. To avoid type mismatch errors,
Example
Usage 1: The following example declares a public variable named userName whose type is String
and assigns an empty string to it:
var userName:String = "";
50Chapter 2: ActionScript Language Reference
Usage 2: The following example shows how to specify a function’s parameter type by defining a
function named
randomInt() that takes a parameter named integer of type Number:
Usage 3: The following example defines a function named squareRoot() that takes a parameter
named
val of the Number type and returns the square root of val, also a Number type:
function squareRoot(val:Number):Number {
return Math.sqrt(val);
}
trace(squareRoot(121));
See Also
var
, function.
: (type)51
?: (conditional)
Availability
Flash Player 4.
Usage
expression1
Parameters
expression1
such as
expression2, expression3 Values of any type.
Returns
The value of expression2 or expression3.
Description
Operator; instructs Flash to evaluate expression1, and if the value of expression1 is true, it
returns the value of
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following statement assigns the value of variable x to variable z because expression1
evaluates to
var x:Number = 5;
var y:Number = 10;
var z = (x < 6) ? x: y;
trace (z); // returns 5
The following example shows a conditional statement written in shorthand:
var timecode:String = (new Date().getHours()<11) ? "AM" : "PM";
trace(timecode);
The same conditional statement could also be written in longhand, as shown in the following
example:
if (new Date().getHours()<11) {
var timecode:String = "AM";
} else {
var timecode:String = "PM";
}
trace(timecode);
?expression2 : expression3
An expression that evaluates to a Boolean value; usually a comparison expression,
x < 5.
expression2; otherwise it returns the value of expression3.
true:
52Chapter 2: ActionScript Language Reference
/ (division)
Availability
Flash Player 4.
Usage
expression1 / expression2
Parameters
expression
Returns
A floating-point number.
Description
Operator (arithmetic); divides expression1 by expression2. The result of the division
operation is a double-precision floating-point number.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following statement divides the current width and height of the Stage, and then displays the
result in the Output panel.
trace(Stage.width/2);
trace(Stage.height/2);
For a default Stage width and height of 550 x 400, the output is 275 and 150.
A number or a variable that evaluates to a number.
See Also
% (modulo)
/ (division)53
// (comment delimiter)
Availability
Flash 1.
Usage
// comment
Parameters
comment
Returns
Nothing.
Description
Comment; indicates the beginning of a script comment. Any characters that appear between the
comment delimiter (
by the ActionScript interpreter.
For more information, see “Deprecated Flash 4 operators” in Using ActionScript in Flash.
Example
The following script uses comment delimiters to identify the first, third, fifth, and seventh lines as
comments:
// record the X position of the ball movie clip
var ballX:Number = ball_mc._x;
// record the Y position of the ball movie clip
var ballY:Number = ball_mc._y;
// record the X position of the bat movie clip
var batX:Number = bat_mc._x;
// record the Y position of the ball movie clip
var batY:Number = bat_mc._y;
Any characters.
//) and the end-of-line character are interpreted as a comment and ignored
See also
/* (comment delimiter)
54Chapter 2: ActionScript Language Reference
/* (comment delimiter)
Availability
Flash Player 5.
Usage
/* comment */
/*
comment
comment
*/
Parameters
comment
Returns
Nothing.
Description
Comment; indicates one or more lines of script comments. Any characters that appear between
the opening comment tag (
and ignored by the ActionScript interpreter. Use the // (comment delimiter) to identify single-line
comments. Use the /* comment delimiter to identify comments on multiple successive lines.
Leaving off the closing tag (
error message. Attempting to nest comments also returns an error message. After an opening
comment tag (/*) is used, the first closing comment tag (
the number of opening comment tags (/*) placed between them.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Any characters.
/*) and the closing comment tag (*/), are interpreted as a comment
*/) when using this form of comment delimiter returns an
*/) will end the comment, regardless of
Example
The following script uses comment delimiters at the beginning of the script:
/* records the X and Y positions of the
ball and bat movie clips */
var ballX:Number = ball_mc._x;
var ballY:Number = ball_mc._y;
var batX:Number = bat_mc._x;
var batY:Number = bat_mc._y;
The following attempt to nest comments will result in an error message:
/* this is an attempt to nest comments.
/* But the first closing tag will be paired with the first opening tag */
and this text will not be interpreted as a comment */
See also
// (comment delimiter)
/* (comment delimiter)55
/= (division assignment)
Availability
Flash Player 4.
Usage
expression1 /= expression2
Parameters
expression1,expression2
Returns
A number.
Description
Operator (arithmetic compound assignment); assigns expression1 the value of expression1/
expression2. For example, the following two statements are equivalent:
x /= y
x = x / y
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following code illustrates using the division assignment (/=) operator with variables and
numbers:
var x:Number = 10;
var y:Number = 2;
x /= y;
trace(x); // output: 5
A number or a variable that evaluates to a number.
See Also
/ (division)
56Chapter 2: ActionScript Language Reference
[] (array access)
Availability
Flash Player 4.
Usage
myArray = [a0, a1,...aN]
myArray[i] = value
myObject[propertyName]
Parameters
myArray
a0, a1,...aN Elements in an array; any native type or object instance, including nested arrays.
i An integer index greater than or equal to 0.
myObject The name of an object.
propertyName A string that names a property of the object.
Returns
Usage 1: A reference to an array.
Usage 2: A value from the array; either a native type or an object instance (including an Array
instance).
Usage 3: A property from the object; either a native type or an object instance (including an Array
instance).
The name of an array.
Description
Operator; initializes a new array or multidimensional array with the specified elements (a0, and so
on), or accesses elements in an array. The array access operator lets you dynamically set and
retrieve instance, variable, and object names. It also lets you access object properties.
Usage 1: An array is an object whose properties are called elements, which are each identified by a
number called an index. When you create an array, you surround the elements with the array
access ([]) operator (or brackets). An array can contain elements of various types. For example, the
following array, called
employee, has three elements; the first is a number and the second two are
strings (inside quotation marks):
var employee:Array = [15, "Barbara", "Jay"];
You can nest brackets to simulate multidimensional arrays. You can nest arrays up to 256 levels
deep. The following code creates an array called
ticTacToe with three elements; each element is
also an array with three elements:
var ticTacToe:Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
// Select Debug > List Variables in test mode
// to see a list of the array elements.
[] (array access)57
Usage 2: Surround the index of each element with brackets ([]) to access it directly; you can add a
new element to an array, or you can change or retrieve the value of an existing element. The first
index in an array is always 0, as shown in the following example:
var my_array:Array = new Array();
my_array[0] = 15;
my_array[1] = "Hello";
my_array[2] = true;
You can use brackets ([]) to add a fourth element, as shown in the following example:
my_array[3] = "George";
You can use brackets ([]) to access an element in a multidimensional array. The first set of brackets
identifies the element in the original array, and the second set identifies the element in the nested
array. The following lines of code send the number 6 to the Output panel.
Usage 3: You can use the array access ([]) operator instead of the eval() function to dynamically
set and retrieve values for movie clip names or any property of an object. The following line of
code sends the number 6 to the Output panel.
name["mc" + i] = "left_corner";
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example shows two ways to create a new empty Array object; the first line uses
brackets ([]):
var my_array:Array = [];
var my_array:Array = new Array();
The following example creates an array called employee_array and uses the trace() statement
to send the elements to the Output panel. In the fourth line, an element in the array is changed,
and the fifth line sends the newly modified array to the Output panel:
In the following example, the expression inside the brackets ("piece" + i) is evaluated and the
result is used as the name of the variable to be retrieved from the
example, the variable
equal to 5, for example, the value of the variable
i must live on the same Timeline as the button. If the variable i is
piece5 in the my_mc movie clip is displayed in
my_mc movie clip. In this
the Output panel:
myBtn_btn.onRelease = function() {
x = my_mc["piece"+i];
trace(x);
};
58Chapter 2: ActionScript Language Reference
In the following example, the expression inside the brackets is evaluated, and the result is used as
the name of the variable to be retrieved from movie clip
name_mc["A" + i];
name_mc:
If you are familiar with the Flash 4 ActionScript slash syntax, you can use the eval() function to
accomplish the same result:
eval("name_mc.A" & i);
You can use the following ActionScript to loop over all objects in the _root scope, which is useful
for debugging:
for (i in _root) {
trace(i+": "+_root[i]);
}
You can also use the array access ([]) operator on the left side of an assignment statement to
dynamically set instance, variable, and object names:
employee_array[2] = "Sam";
See also
Array class, Object class
[] (array access)59
^ (bitwise XOR)
Availability
Flash Player 5.
Usage
expression1 ^ expression2
Parameters
expression1,expression2
Returns
A 32-bit integer.
Description
Operator (bitwise); converts expression1 and expression2 to 32-bit unsigned integers, and
returns a 1 in each bit position where the corresponding bits in
but not both, are 1. Floating-point numbers are converted to integers by discarding any digits
after the decimal point. The result is a new 32-bit integer.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
The return value is interpreted as a two’s complement number with sign, so the return value will
be an integer in the range -2147483648 to 2147483647.
A number.
expression1 or expression2,
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example uses the bitwise XOR operator on the decimals 15 and 9, and assigns the
result to the variable
value1,2,...N The corresponding values for each name property.
Returns
Usage 1: An Object object.
Usage 2: Nothing, except when a function has an explicit
return type is specified in the function implementation.
Description
Operator; creates a new object and initializes it with the specified name and value property pairs.
Using this operator is the same as using the
using the assignment operator. The prototype of the newly created object is generically named the
Object object.
The object to create.
return statement, in which case the
new Object syntax and populating the property pairs
This operator is also used to mark blocks of contiguous code associated with flow control
statements (
for, while, if, else, switch) and functions.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The first line of the following code creates an empty object using the object initializer ({})
operator; the second line creates a new object using a constructor function:
var object:Object = {};
var object:Object = new Object();
The following example creates an object account and initializes the properties name, address,
city, state, zip, and balance with accompanying values:
var account:Object = {name:"Macromedia, Inc.",
address:"600 Townsend Street",
city:"San Francisco",
state:"California",
zip:"94103",
balance:"1000"};
for (i in account) {
trace("account."+i+" = "+account[i]);
}
62Chapter 2: ActionScript Language Reference
The following example shows how array and object initializers can be nested within each other:
var person:Object = {name:"Gina Vechio",
children:["Ruby", "Chickie", "Puppa"]};
The following example uses the information in the previous example and produces the same
result using constructor functions:
var person:Object = new Object();
person.name = "Gina Vechio";
person.children = new Array();
person.children[0] = "Ruby";
person.children[1] = "Chickie";
person.children[2] = "Puppa";
The previous ActionScript example can also be written in the following format:
var person:Object = new Object();
person.name = "Gina Vechio";
person.children = new Array("Ruby", "Chickie", "Puppa");
See also
[] (array access)
, new, Object class
{} (object initializer)63
| (bitwise OR)
Availability
Flash Player 5.
Usage
expression1 | expression2
Parameters
expression1,expression2
Returns
A 32-bit integer.
Description
Operator (bitwise); converts expression1 and expression2 to 32-bit unsigned integers, and
returns a 1 in each bit position where the corresponding bits of either
expression2 are 1. Floating-point numbers are converted to integers by discarding any digits
after the decimal point. The result is a new 32-bit integer.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
The return value is interpreted as a two’s complement number with sign, so the return value will
be an integer in the range -2147483648 to 2147483647.
A number.
expression1 or
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following is an example of a bitwise OR (|) operation:
Operator (logical); evaluates expression1 (the expression on the left side of the operator) and
returns
true if the expression evaluates to true. If expression1 evaluates to false,
expression2 (the expression on the right side of the operator) is evaluated. If expression2
evaluates to
false, the final result is false; otherwise, it is true.
If you use a function call as
expression1 evaluates to true.
The result is
true if either or both expressions evaluate to true; the result is false only if both
expressions evaluate to
if any operand evaluates to
A Boolean value or an expression that converts to a Boolean
expression2, the function will not be executed by that call if
false. You can use the logical OR operator with any number of operands;
true, the result is true.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example uses the logical OR (||) operator in an if statement. The second
expression evaluates to
var x:Number = 10;
var y:Number = 250;
var start:Boolean = false;
if ((x>25) || (y>200) || (start)) {
trace("the logical OR test passed"); // output: the logical OR test passed
}
true, so the final result is true:
The message the logical OR test passed appears because one of the conditions in the if
statement is true (
condition evaluates to
The following example demonstrates how using a function call as
unexpected results. If the expression on the left of the operator evaluates to
returned without evaluating the expression on the right (the function
function fx1():Boolean {
trace("fx1 called");
return true;
y>200). Although the other two expressions evaluate to false, as long as one
true, the if block executes.
expression2 can lead to
true, that result is
fx2() is not called).
|| (logical OR)65
}
function fx2():Boolean {
trace("fx2 called");
return true;
}
if (fx1() || fx2()) {
trace("IF statement entered");
}
/* The following is sent to the Output panel:
Operator (bitwise); also known as the one’s complement operator or the bitwise complement
operator. Converts the
complement. That is, every bit that is a 0 is set to 1 in the result, and every bit that is a 1 is set to
0 in the result. The result is a signed 32-bit integer.
For example, the hex value 0x7777 is represented as this binary number:
0111011101110111
The bitwise negation of that hex value, ~0x7777, is this binary number:
1000100010001000
In hexadecimal, this is 0x8888. Therefore, ~0x7777 is 0x8888.
A number.
expression to a 32-bit signed integer, and then applies a bitwise one’s
The most common use of bitwise operators is for representing flag bits (Boolean values packed
into 1 bit each).
Floating-point numbers are converted to integers by discarding any digits after the decimal point.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
The return value is interpreted as a two’s complement number with sign, so the return value is an
integer in the range -2147483648 to 2147483647.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example demonstrates a use of the bitwise NOT (-) operator with flag bits:
var ReadOnlyFlag:Number = 0x0001;
// defines bit 0 as the read-only flag
var flags:Number = 0;
trace(flags);
68Chapter 2: ActionScript Language Reference
/* To set the read-only flag in the flags variable, the following code uses the
bitwise OR: */
flags |= ReadOnlyFlag;
trace(flags);
/* To clear the read-only flag in the flags variable, first construct a mask by
using bitwise NOT on ReadOnlyFlag. In the mask, every bit is a 1 except for
the read-only flag. Then, use bitwise AND with the mask to clear the read-
only flag. The following code constructs the mask and performs the bitwise
Flash Player 4; Flash Player 5.In Flash 5 and later, + is either a numeric operator or a string
concatenator depending on the data type of the parameter. In Flash 4,
operator. Flash 4 files that are brought into the Flash 5 or later authoring environment undergo a
conversion process to maintain data type integrity. The following example illustrates the
conversion of a Flash 4 file containing a numeric quality comparison:
Flash 4 file:
x + y
Converted Flash 5 or later file:
Number(x) + Number(y)
Usage
expression1 + expression2
Parameters
expression1,expression2
Returns
A number or string.
A string, integer, or floating-point number.
Description
Operator; adds numeric expressions or concatenates (combines) strings. If one expression is a
string, all other expressions are converted to strings and concatenated.
+ is only a numeric
If both expressions are integers, the sum is an integer; if either or both expressions are floatingpoint numbers, the sum is a floating-point number.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
Usage 1: The following example concatenates two strings and displays the result in the
Output panel.
var name:String = "Cola";
var instrument:String = "Drums";
trace(name+" plays "+instrument); // output: Cola plays Drums
Usage 2: This statement adds the integers 2 and 3 and displays the resulting integer, 5, in the
Output panel:
trace(2+3); // output: 5
This statement adds the floating-point numbers 2.5 and 3.25 and displays the resulting floatingpoint number, 5.75, in the Output panel
trace(2.5+3.25); // output: 5.75
70Chapter 2: ActionScript Language Reference
Usage 3: Variables associated with dynamic and input text fields have the data type String. In the
following example, the variable
deposit amount, the script attempts to add
deposit is an input text field on the Stage. After a user enters a
deposit to oldBalance. However, because deposit
is a String data type, the script concatenates (combines to form one string) the variable values
rather than summing them.
var oldBalance:Number = 1345.23;
var currentBalance = deposit_txt.text + oldBalance;
trace(currentBalance);
For example, if a user enters 475 in the deposit text field, the trace() statement sends the value
4751345.23 to the Output panel.
To correct this, use the
var oldBalance:Number = 1345.23;
var currentBalance:Number = Number(deposit_txt.text) + oldBalance;
trace(currentBalance);
Number() function to convert the string to a number, as in the following:
The following example shows how numeric sums to the right of a string expression are not
calculated:
Operator (arithmetic compound assignment); assigns expression1 the value of expression1 +
expression2
x += y;
x = x + y;
. For example, the following two statements have the same result:
This operator also performs string concatenation. All the rules of the addition (+) operator apply
to the addition assignment
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
Usage 1: This example uses the += operator with a string expression and sends “My name is
Gilbert” to the Output panel.
var x1:String = "My name is ";
x1 += "Gilbert";
trace(x1); // output: My name is Gilbert
Usage 2: The following example shows a numeric use of the addition assignment (+=) operator:
var x:Number = 5;
var y:Number = 10;
x += y;
trace(x); // output: 15
A number or string.
(+=) operator.
See also
+ (addition)
72Chapter 2: ActionScript Language Reference
< (less than)
Availability
Flash Player 4; Flash Player 5. In Flash 5 and later, the < (less than) operator is a comparison
operator capable of handling various data types. In Flash 4,
that are brought into the Flash 5 or later authoring environment undergo a conversion process to
maintain data type integrity. The following illustrates the conversion of a Flash 4 file containing a
numeric quality comparison.
Flash 4 file:
x < y
Converted Flash 5 or later file:
Number(x) < Number(y)
Usage
expression1 < expression2
Parameters
expression1,expression2
Returns
A number or string.
A Boolean value.
Description
Operator (comparison); compares two expressions and determines whether expression1 is less
than
expression2; if so, the operator returns true. If expression1 is greater than or equal to
expression2, the operator returns false. String expressions are evaluated using alphabetical
order; all capital letters come before lowercase letters.
< is an numeric operator. Flash 4 files
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following examples show true and false returns for both numeric and string comparisons:
expression2 A number or expression that converts to an integer from 0 to 31.
Returns
A 32-bit integer.
Description
Operator (bitwise); converts expression1 and expression2 to 32-bit integers, and shifts all the
bits in
conversion of
filled in with 0 and bits shifted off the left end are discarded. Shifting a value left by one position
is the equivalent of multiplying it by 2.
Floating-point numbers are converted to integers by discarding any digits after the decimal point.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
The return value is interpreted as a two’s complement number with sign, so the return value will
be an integer in the range -2147483648 to 2147483647.
<<expression2
A number or expression to be shifted left.
expression1 to the left by the number of places specified by the integer resulting from the
expression2. The bit positions that are emptied as a result of this operation are
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
In the following example, the integer 1 is shifted 10 bits to the left:
x = 1 << 10
The result of this operation is x = 1024. This is because 1 decimal equals 1 binary, 1 binary
shifted left by 10 is 10000000000 binary, and 10000000000 binary is 1024 decimal.
In the following example, the integer 7 is shifted 8 bits to the left:
x = 7 << 8
The result of this operation is x = 1792. This is because 7 decimal equals 111 binary, 111 binary
shifted left by 8 bits is 11100000000 binary, and 11100000000 binary is 1792 decimal.
74Chapter 2: ActionScript Language Reference
If you trace the following example, you see that the bits have been pushed two spaces to the left:
>>= (bitwise right shift and assignment), >> (bitwise right shift), <<= (bitwise left shift and
assignment), >>> (bitwise unsigned right shift), >>>= (bitwise unsigned right shift and
assignment)
<< (bitwise left shift)75
<<= (bitwise left shift and assignment)
Availability
Flash Player 5.
Usage
expression1 <<= expression2
Parameters
expression1
expression2 A number or expression that converts to an integer from 0 to 31.
Returns
A 32-bit integer.
Description
Operator (bitwise compound assignment); this operator performs a bitwise left shift (<<=)
operation and stores the contents as a result in
equivalent:
A <<= B
A = (A << B)
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
In the following example, you use the bitwise left shift and assignment (<<=) operator to shift all
bits one space to the left:
var x:Number = 4;
// shift all bits one slot to the left.
x <<= 1;
trace(x); // output: 8
// 4 decimal = 0100 binary
// 8 decimal = 1000 binary
A number or expression to be shifted left.
expression1. The following two expressions are
See also
<< (bitwise left shift), >>= (bitwise right shift and assignment), >> (bitwise right shift)
76Chapter 2: ActionScript Language Reference
<= (less than or equal to)
Availability
Flash Player 4.
In Flash 5 or later, the less than or equal to (
handling various data types. In Flash 4,
into the Flash 5 or later authoring environment undergo a conversion process to maintain data
type integrity. The following illustrates the conversion of a Flash 4 file containing a numeric
quality comparison.
Flash 4 file:
x <= y
Converted Flash 5 or later file:
Number(x) <= Number(y)
Usage
expression1 <= expression2
Parameters
expression1,expression2
Returns
A number or string.
A Boolean value.
Description
Operator (comparison); compares two expressions and determines whether expression1 is less
than or equal to
expression2, the operator returns false. String expressions are evaluated using alphabetical
expression2; if it is, the operator returns true. If expression1 is greater than
order; all capital letters come before lowercase letters.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
<=) operator is a comparison operator capable of
<= is a numeric operator. Flash 4 files that are brought
Example
The following examples show true and false results for both numeric and string comparisons:
= is a numeric equality operator. Flash 4 files that are brought into the Flash 5 or later
authoring environment undergo a conversion process to maintain data type integrity.
Flash 4 file:
x = y
Converted Flash 5 or later file:
Number(x) == Number(y)
Usage
expression1 = expression2
Parameters
expression1
expression2 A value of any type.
Returns
The assigned value, expression2.
Description
Operator; assigns the value of expression2 (the parameter on the right) to the variable, array
element, or property in
Assignment by value copies the actual value of
Assignment by value is used when a variable is assigned a number or string literal. Assignment by
reference stores a reference to
commonly used with the
a reference to that location in memory is assigned to a variable.
= is an assignment operator, and the == operator is used to evaluate equality. In
A variable, element of an array, or property of an object.
expression1. Assignment can be either by value or by reference.
expression2 and stores it in expression1.
expression2 in expression1. Assignment by reference is
new operator. Use of the new operator creates an object in memory and
For more information, see “Operator precedence and associativity” in Using ActionScript in
Flash.
Example
The following example uses assignment by value to assign the value of 5 to the variable x.
var x:Number = 5;
The following example uses assignment by value to assign the value "hello" to the variable x:
var x:String;
x = "hello";
78Chapter 2: ActionScript Language Reference
The following example uses assignment by reference to create the moonsOfJupiter variable,
which contains a reference to a newly created Array object. Assignment by value is then used to
copy the value
moonsOfJupiter:
var moonsOfJupiter:Array = new Array();
moonsOfJupiter[0] = "Callisto";
"Callisto" to the first element of the array referenced by the variable
The following example uses assignment by reference to create a new object, and assign a reference
to that object to the variable
neptune. Assignment by value is then used to assign the value of
49528 to the size property of the myObject object:
var mercury:Object = new Object();
mercury.diameter = 3030; // in miles
trace (mercury.diameter); // output: 3030
The following example builds upon the previous example by creating a variable named merkur
(the German word for mercury) and assigning it the value of
mercury. This creates two variables
that reference the same object in memory, which means you can use either variable to access the
object’s properties. We can then change the diameter property to use kilometers instead of miles:
var merkur:Object = mercury;
merkur.diameter = 4878; // in kilometers
trace (mercury.diameter); // output: 4878
See also
== (equality)
= (assignment)79
-= (subtraction assignment)
Availability
Flash Player 4.
Usage
expression1 -= expression2
Parameters
expression1,expression2
Returns
A number.
Description
Operator (arithmetic compound assignment); assigns expression1 the value of expression1 -
expression2. For example, the following two statements are equivalent:
x -= y;
x = x - y;
String expressions must be converted to numbers; otherwise, NaN (not a number) is returned.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example uses the subtraction assignment (-=) operator to subtract 10 from 5 and
assign the result to the variable
var x:Number = 5;
var y:Number = 10;
x -= y;
trace(x); // output: -5
The following example shows how strings are converted to numbers:
var x:String = "5";
var y:String = "10";
x -= y;
trace(x); // output: -5
A number or expression that evaluates to a number.
x:
See also
– (minus)
80Chapter 2: ActionScript Language Reference
== (equality)
Availability
Flash Player 5.
Usage
expression1 == expression2
Parameters
expression1,expression2
A number, string, Boolean value, variable, object, array,
or function.
Returns
A Boolean value.
Description
Operator (equality); tests two expressions for equality. The result is true if the expressions
are equal.
The definition of equal depends on the data type of the parameter:
• Numbers and Boolean values are compared by value and are considered equal if they have the
same value.
• String expressions are equal if they have the same number of characters and the characters
are identical.
• Variables representing objects, arrays, and functions are compared by reference. Two such
variables are equal if they refer to the same object, array, or function. Two separate arrays are
never considered equal, even if they have the same number of elements.
When comparing by value, if
ActionScript will attempt to convert the data type of
expression1. For more information, see “Automatic data typing” and “Operator precedence and
expression1 and expression2 are different data types,
expression2 to match that of
associativity” in Using ActionScript in Flash.
Example
The following example uses the equality (==) operator with an if statement:
var a:String = "David", b:String = "David";
if (a == b) {
trace("David is David");
}
The following examples show the results of operations that compare mixed types:
var x:Number = 5;
var y:String = "5";
trace(x == y); // output: true
var x:String = "5";
var y:String = "66";
trace(x == y); // output: false
var x:String = "chris";
== (equality)81
var y:String = "steve";
trace(x == y); // output: false
The following examples show comparison by reference. The first example compares two arrays
with identical length and elements. The equality operator will return
false for these two arrays.
Although the arrays appear equal, comparison by reference requires that they both refer to the
same array. The second example creates the
as the variable
firstArray. The equality operator will return true for these two arrays because
thirdArray variable, which points to the same array
the two variables refer to the same array.
var firstArray:Array = new Array("one", "two", "three");
var secondArray:Array = new Array("one", "two", "three");
trace(firstArray == secondArray); // will output false
// Arrays are only considered equal
// if the variables refer to the same array.
var thirdArray:Array = firstArray;
trace(firstArray == thirdArray); // will output true
Operator; tests two expressions for equality; the strict equality (===)operator performs in the same
way as the equality (
==) operator, except that data types are not converted. (For more
information, see “Automatic data typing” in Using ActionScript in Flash.) The result is
both expressions, including their data types, are equal.
The definition of equal depends on the data type of the parameter:
• Numbers and Boolean values are compared by value and are considered equal if they have the
same value.
• String expressions are equal if they have the same number of characters and the characters
are identical.
• Variables representing objects, arrays, and functions are compared by reference. Two such
variables are equal if they refer to the same object, array, or function. Two separate arrays are
never considered equal, even if they have the same number of elements.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
true if
Example
The comments in the following code show the returned value of operations that use the equality
and strict equality operators:
// Both return true because no conversion is done
var string1:String = "5";
var string2:String = "5";
trace(string1 == string2); // true
trace(string1 === string2); // true
// Automatic data typing in this example converts 5 to “5”
var string1:String = "5";
var num:Number = 5;
trace(string1 == num); // true
trace(string1 === num); // false
// Automatic data typing in this example converts true to “1”
var string1:String = "1";
var bool1:Boolean = true;
trace(string1 == bool1); // true
trace(string1 === bool1); // false
=== (strict equality)83
// Automatic data typing in this example converts false to “0”
var string1:String = "0";
var bool2:Boolean = false;
trace(string1 == bool2); // true
trace(string1 === bool2); // false
The following examples show how strict equality treats variables that are references differently
than it treats variables that contain literal values. This is one reason to consistently use String
literals and to avoid the use of the
// Create a string variable using a literal value
var str:String = "asdf”;
// Create a variable that is a reference
var stringRef:String = new String("asdf");
// The equality operator does not distinguish among literals, variables,
// and references
trace(stringRef == "asdf"); // true
trace(stringRef == str); // true
trace("asdf" == str); // true
// The strict equality operator considers variables that are references
// distinct from literals and variables
trace(stringRef === "asdf"); // false
trace(stringRef === str); // false
In Flash 5 or later, the greater-than (
various data types. In Flash 4,
5 or later authoring environment undergo a conversion process to maintain data type integrity.
The following illustrates the conversion of a Flash 4 file containing a numeric quality comparison.
Flash 4 file:
x > y
Converted Flash 5 or later file:
Number(x) > Number(y)
Usage
expression1 >expression2
Parameters
expression1,expression2
Returns
A Boolean value.
Description
Operator (comparison); compares two expressions and determines whether expression1 is
greater than
to
expression2, the operator returns false. String expressions are evaluated using alphabetical
expression2; if it is, the operator returns true. If expression1 is less than or equal
order; all capital letters come before lowercase letters.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
>) operator is a comparison operator capable of handling
> is a numeric operator. Flash 4 files that are brought into the Flash
A number or string.
Example
In the following example, the greater than (>) operator is used to determine whether the value of
the text field
if (score_txt.text>90) {
trace("Congratulations, you win!");
} else {
trace("sorry, try again");
}
score_txt is greater than 90:
> (greater than)85
>= (greater than or equal to)
Availability
Flash Player 4.
In Flash 5 or later, the greater than or equal to (
handling various data types. In Flash 4,
>= is a numeric operator. Flash 4 files that are brought
into the Flash 5 or later authoring environment undergo a conversion process to maintain data
type integrity. The following illustrates the conversion of a Flash 4 file containing a numeric
quality comparison.
Flash 4 file:
x > y
Converted Flash 5 or later file:
Number(x) > Number(y)
Usage
expression1 >= expression2
Parameters
expression1, expression2
Returns
A string, integer, or floating-point number.
A Boolean value.
Description
Operator (comparison); compares two expressions and determines whether expression1 is
greater than or equal to
expression2 (true) or expression1 is less than expression2 (false).
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
>=) operator is a comparison operator capable of
Example
In the following example, the greater than or equal to (>=) operator is used to determine whether
the current hour is greater than or equal to 12:
if (new Date().getHours()>=12) {
trace("good afternoon");
} else {
trace("good morning");
}
86Chapter 2: ActionScript Language Reference
>> (bitwise right shift)
Availability
Flash Player 5.
Usage
expression1 >> expression2
Parameters
expression1
expression2 A number or expression that converts to an integer from 0 to 31.
Returns
A 32-bit integer.
Description
Operator (bitwise); converts expression1 and expression2 to 32-bit integers, and shifts all the
bits in
the conversion of
the sign of the original
bit (the bit farthest to the left) of
bit is 1. Shifting a value right by one position is the equivalent of dividing by 2 and discarding
the remainder.
Floating-point numbers are converted to integers by discarding any digits after the decimal point.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
A number or expression to be shifted right.
expression1 to the right by the number of places specified by the integer that results from
expression2. Bits that are shifted off the right end are discarded. To preserve
expression, the bits on the left are filled in with 0 if the most significant
expression1 is 0, and filled in with 1 if the most significant
The return value is interpreted as a two’s complement number with sign, so the return value will
be an integer in the range -2147483648 to 2147483647.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example converts 65535 to a 32-bit integer and shifts it 8 bits to the right:
var x:Number = 65535 >> 8;
trace(x); // outputs 255
The following example shows the result of the previous example:
var x:Number = 255
This is because 65535 decimal equals 1111111111111111 binary (sixteen 1’s),
1111111111111111 binary shifted right by 8 bits is 11111111 binary, and 11111111 binary is
255 decimal. The most significant bit is 0 because the integers are 32-bit, so the fill bit is 0.
>> (bitwise right shift)87
The following example converts -1 to a 32-bit integer and shifts it 1 bit to the right:
var x:Number = -1 >> 1;
trace(x); // outputs -1
The following example shows the result of the previous example:
var x:Number = -1
This is because -1 decimal equals 11111111111111111111111111111111 binary (thirty-two
1’s), shifting right by one bit causes the least significant (bit farthest to the right) to be discarded
and the most significant bit to be filled in with 1. The result is
11111111111111111111111111111111 (thirty-two 1’s) binary, which represents the 32-bit
integer -1.
See also
>>= (bitwise right shift and assignment)
88Chapter 2: ActionScript Language Reference
>>= (bitwise right shift and assignment)
Availability
Flash Player 5.
Usage
expression1 >>= expression2
Parameters
expression1
expression2 A number or expression that converts to an integer from 0 to 31.
Returns
A 32-bit integer.
Description
Operator (bitwise compound assignment); this operator performs a bitwise right-shift operation
and stores the contents as a result in
The following two expressions are equivalent:
A >>= B
A = (A >> B)
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following commented code uses the bitwise right shift and assignment (>>=) operator.
function convertToBinary(numberToConvert:Number):String {
var result:String = "";
for (var i = 0; i<32; i++) {
// Extract least significant bit using bitwise AND
var lsb:Number = numberToConvert & 1;
// Add this bit to the result string
result = (lsb ? "1" : "0")+result;
// Shift numberToConvert right by one bit, to see next bit
numberToConvert >>= 1;
}
return result;
}
trace(convertToBinary(479));
// Returns the string 00000000000000000000000111011111
// This string is the binary representation of the decimal
// number 479
A number or expression to be shifted right.
expression1.
See also
>> (bitwise right shift)
>>= (bitwise right shift and assignment)89
>>> (bitwise unsigned right shift)
Availability
Flash Player 5.
Usage
expression1 >>> expression2
Parameters
expression1
expression2 A number or expression that converts to an integer between 0 and 31.
Returns
A 32-bit unsigned integer.
Description
Operator (bitwise); the same as the bitwise right shift (>>) operator except that it does not
preserve the sign of the original
Floating-point numbers are converted to integers by discarding any digits after the decimal point.
Positive integers are converted to an unsigned hex value with a maximum value of 4294967295 or
0xFFFFFFFF; values larger than the maximum have their most significant digits discarded when
they are converted so the value is still 32-bit. Negative numbers are converted to an unsigned hex
value via the two’s complement notation, with the minimum being -2147483648 or
0x800000000; numbers less than the minimum are converted to two’s complement with greater
precision and also have the most significant digits discarded.
A number or expression to be shifted right.
expression because the bits on the left are always filled with 0.
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
Example
The following example converts -1 to a 32-bit integer and shifts it 1 bit to the right:
var x:Number = -1 >>> 1;
trace(x); // output: 2147483647
This is because -1 decimal is 11111111111111111111111111111111 binary (thirty-two 1’s),
and when you shift right (unsigned) by 1 bit, the least significant (rightmost) bit is discarded, and
the most significant (leftmost) bit is filled with a 0. The result is
01111111111111111111111111111111 binary, which represents the 32-bit integer
2147483647.
See also
>>= (bitwise right shift and assignment)
90Chapter 2: ActionScript Language Reference
>>>= (bitwise unsigned right shift and assignment)
Availability
Flash Player 5.
Usage
expression1 >>>= expression2
Parameters
expression1
expression2 A number or expression that converts to an integer from 0 to 31.
Returns
A 32-bit integer.
Description
Operator (bitwise compound assignment); performs an unsigned bitwise right-shift operation
and stores the contents as a result in
A >>>= B
A = (A >>> B)
For more information, see “Operator precedence and associativity” in Using ActionScript in Flash.
See also
>>> (bitwise unsigned right shift), >>= (bitwise right shift and assignment)
A number or expression to be shifted left.
expression1. The following two expressions are equivalent:
>>>= (bitwise unsigned right shift and assignment)91
CHAPTER 2
ActionScript Language Reference
Accessibility class
Availability
Flash Player 6.
Description
The Accessibility class manages communication with screen readers. Screen readers are a type of
assistive technology for visually impaired users that provides an audio version of screen content.
The methods of the Accessibility class are static—that is, you don’t have to create an instance of
the class to use its methods.
To get and set accessible properties for a specific object, such as a button, movie clip, or text field,
use the
_accProps property. To determine whether the player is running in an environment that
supports accessibility aids, use
creating accessible content, see “Accessibility overview” in Using Flash.
Method summary for the Accessibility class
MethodDescription
Accessibility.isActive()
Accessibility.updateProperties()
System.capabilities.hasAccessibility. For information on
Indicates whether a screen reader program is active.
Updates the description of objects on the screen for screen
readers.
92Chapter 2: ActionScript Language Reference
Accessibility.isActive()
Availability
Flash Player 6.
Usage
Accessibility.isActive() : Boolean
Parameters
None.
Returns
A Boolean value: true if the Flash Player is communicating with an accessibility aid (usually a
screen reader);
Description
Method; indicates whether an accessibility aid is currently active and the player is communicating
with it. Use this method when you want your application to behave differently in the presence of
a screen reader or other accessibility aid.
Note: If you call this method within one or two seconds of the first appearance of the Flash window in
which your document is playing, you might get a return value of
Microsoft Active Accessibility (MSAA) client. This is because of an asynchronous communication
mechanism between Flash and MSAA clients. You can work around this limitation by ensuring a
delay of one to two seconds after loading your document before calling this method.
Example
The following example checks whether an accessibility aid is currently active:
if (Accessibility.isActive()) {
trace ("An accessibility aid is currently active");
} else {
trace ("There is currently no active accessibility aid");
instanceName The instance name assigned to an instance of a movie clip, button, dynamic text
field, or input text field. To refer to the
document, omit
Description
Property; lets you control screen reader accessibility options for SWF files, movie clips, buttons,
dynamic text fields, and input text fields at runtime. These properties override the corresponding
settings available in the Accessibility panel during authoring. For changes to these properties to
take effect, you must call
Accessibility panel, see “The Flash Accessibility panel” in Using Flash.
To determine whether the player is running in an environment that supports accessibility aids, use
System.capabilities.hasAccessibility.
The following table lists the name and data type of each
setting in the Accessibility panel, and the kinds of objects to which the property can be applied.
The term inverse logic means that the property setting is the inverse of the corresponding setting
in the Accessibility panel. For example, setting the
deselecting the Make Movie Accessible or Make Object Accessible option.
An accessibility property name (see the following description for valid names).
_accProps object that represents the entire Flash
instanceName.
Accessibility.updateProperties(). For information on the
_accProps property, its equivalent
silent property to true is equivalent to
PropertyData typeEquivalent in Accessibility panelApplies to
silent
forceSimple
name
BooleanMake Movie Accessible/
Make Object Accessible
(inverse logic)
BooleanMake Child Objects Accessible
(inverse logic)
StringNameWhole SWF files
Whole SWF files
Movie clips
Buttons
Dynamic text
Input text
Whole SWF files
Movie clips
Movie clips
Buttons
Input text
_accProps95
PropertyData typeEquivalent in Accessibility panelApplies to
description
shortcut
*For the Shortcut field, use names of the form Control+A. Adding a keyboard shortcut to the Accessibility panel
doesn’t create a keyboard shortcut; it merely advises screen readers of an existing shortcut. For information on
assigning a keyboard shortcut to an accessible object, see Key.addListener().
StringDescriptionWhole SWF files
Movie clips
Buttons
Dynamic text
Input text
StringShortcut*Movie clips
Buttons
Input text
To specify settings that correspond to the Tab index setting in the Accessibility panel, use the
Button.tabIndex, MovieClip.tabIndex, or TextField.tabIndex property.
There is no way to specify an Auto Label setting at runtime.
To refer to the
instanceName parameter. The value of _accProps must be an object. This means that if no
_accProps object already exists, you must create one, as shown in the following example, before
you can assign values to the properties of the
if ( _accProps == undefined ) {
_accProps = new Object();
}
_accProps.name = "My SWF file";
_accProps object that represents the entire Flash document, omit the
_accProps object:
When _accProps is used without the instanceName parameter, changes made to _accProps
properties apply to the whole SWF file. For example, the following code sets the Accessibility
name property for the whole SWF file to the string "Pet Store" and then calls
Accessibility.updateProperties() to cause that change:
If you are specifying several accessibility properties, make as many changes as you can
before calling
Accessibility.updateProperties(), instead of calling it after each
property statement, as shown in the following example:
_accProps.name = "Pet Store";
animal_mc._accProps.name = "Animal";
animal_mc._accProps.description = "Cat, dog, fish, etc.";
price_mc._accProps.name = "Price";
price_mc._accProps.description = "Cost of a single item";
Accessibility.updateProperties();
96Chapter 2: ActionScript Language Reference
If you don’t specify an accessibility property for a document or an object, any values set in the
Accessibility panel are implemented.
After you specify an accessibility property, you can’t revert its value to a value set in the
Accessibility panel. However, you can set the property to its default value (
values; empty strings for string values) by deleting the property from the
false for Boolean
_accProps object, as
shown in the following example:
my_mc._accProps.silent = true; // set a property
// other code here
delete my_mc._accProps.silent; // revert to default value
The value of _accProps must be an object. This means that if no _accProps object already exists,
you must create one before you can assign clues to the properties of the
if (_accProps == undefined) {
_accProps = new Object();
}
_accProps.name = "My movie";
Example
_accProps object.
If you change an image and want to update its accessibility description, you can use the following
ActionScript code:
my_mc.gotoAndStop(2);
if (my_mc._accProps == undefined ) {
my_mc._accProps = new Object();
}
my_mc._accProps.name = "Photo of Mount Rushmore";
Accessibility.updateProperties();
The arguments object is an array that contains the values that were passed as parameters to any
function. Each time a function is called in ActionScript, an arguments object is automatically
created for that function. A local variable,
arguments object.
Property summary for the arguments object
PropertyDescription
arguments.callee
arguments.caller
arguments.length
A reference to the function being called.
A reference to the calling function.
The number of parameters passed to a function.
arguments, is also created and lets you refer to the
98Chapter 2: ActionScript Language Reference
arguments.callee
Availability
Flash Player 5.
Usage
arguments.callee:Function
Description
Property; refers to the function that is currently being called.
Example
You can use the arguments.callee property to make an anonymous function that is recursive, as
shown in the following example:
factorial = function (x:Number) {
if (x<=1) {
return 1;
} else {
return x*arguments.callee(x-1);
}
};
trace(factorial(3)); // output: 6
The following example is a named recursive function:
function factorial(x:Number):Number {
if (x<=1) {
return 1;
} else {
return x*factorial(x-1);
}
}
trace(factorial(4)); // output: 24
arguments.callee99
arguments.caller
Availability
Flash Player 6.
Usage
arguments.caller:Function
Description
Property; refers to the calling function. The value of this property is null if the current function
was not called by another function.
Example
The following example defines two functions—a caller function, named function1, which calls a
another function, named function2:
// define the caller function, named function1
var function1:Function = function () {
function2("hello from function1");
}
// define the callee function, named function2
var function2:Function = function (aString:String) {
if (arguments.caller == function1) {
trace("function2 was called by function1");
trace(aString);
}
}
// call function1
function1();
/*
Output:
function2 was called by function1
hello from function1
*/
100Chapter 2: ActionScript Language Reference
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.