Writing Scripts with ActionScript > About data types
About data typesA data type describes the kind of information a variable or ActionScript element can hold. There are two kinds of data types: primitive and reference. The primitive data typesstring, number, and Booleanhave a constant value and, therefore, can hold the actual value of the element they represent. The reference data typesmovie clip and objecthave values that can change and, therefore, contain references to the actual value of the element. Variables containing primitive data types behave differently in certain situations than those containing reference types. See Using variables in a script.
Each data type has its own rules and is listed here. References are included for data types that are discussed in more detail.
A string is a sequence of characters such as letters, numbers, and punctuation marks. You enter strings in an ActionScript statement by enclosing them in single or double quotation marks. Strings are treated as characters instead of as variables. For example, in the following statement, "L7"
is a string:
favoriteBand = "L7";
You can use the addition (+)
operator to concatenate, or join, two strings. ActionScript treats spaces at the beginning or end of a string as a literal part of the string. The following expression includes a space after the comma:
greeting = "Welcome," + firstName;
Although ActionScript does not distinguish between uppercase and lowercase in references to variables, instance names, and frame labels, literal strings are case sensitive. For example, the following two statements place different text into the specified text field variables, because "Hello"
and "HELLO"
are literal strings.
invoice.display = "Hello"; invoice.display = "HELLO";
To include a quotation mark in a string, precede it with a backslash character (\). This is called "escaping" a character. There are other characters that cannot be represented in ActionScript except by special escape sequences. The following table provides all the ActionScript escape characters:
Escape sequence | Character |
---|---|
|
Backspace character (ASCII 8) |
|
Form-feed character (ASCII 12) |
|
Line-feed character (ASCII 10) |
|
Carriage return character (ASCII 13) |
|
Tab character (ASCII 9) |
|
Double quotation mark |
|
Single quotation mark |
|
Backslash |
|
A byte specified in octal |
|
A byte specified in hexadecimal |
|
A 16-bit Unicode character specified in hexadecimal |
The number data type is a double-precision floating-point number. You can manipulate numbers using the arithmetic operators addition (+), subtraction (-), multiplication (*), division (/), modulo (%), increment (++), and decrement (--). You can also use methods of the predefined Math object to manipulate numbers. The following example uses the sqrt
(square root) method to return the square root of the number 100:
Math.sqrt(100);
See Numeric operators.
A Boolean value is one that is either true
or false
. ActionScript also converts the values true
and false
to 1 and 0 when appropriate. Boolean values are most often used with logical operators in ActionScript statements that make comparisons to control the flow of a script. For example, in the following script, the movie plays if the variable password
is true
:
onClipEvent(enterFrame) { if ((userName == true) && (password == true)){ play(); } }
See Using "if" statements and Logical operators.
An object is a collection of properties. Each property has a name and a value. The value of a property can be any Flash data type, even the object data type. This allows you to arrange objects inside each other, or "nest" them. To specify objects and their properties, you use the dot (.) operator. For example, in the following code, hoursWorked
is a property of weeklyStats
, which is a property of employee
:
employee.weeklyStats.hoursWorked
You can use ActionScript's predefined objects to access and manipulate specific kinds of information. For example, the Math object has methods that perform mathematical operations on numbers you pass to them. This example uses the sqrt
method:
squareRoot = Math.sqrt(100);
The ActionScript MovieClip object has methods that let you control movie clip symbol instances on the Stage. This example uses the play
and nextFrame
methods:
mcInstanceName.play(); mc2InstanceName.nextFrame();
You can also create your own objects so that you can organize information in your movie. To add interactivity to a movie with ActionScript, you'll need many different pieces of information: for example, you might need a user's name, the speed of a ball, the names of items in a shopping cart, the number of frames loaded, the user's zip code, and which key was pressed last. Creating custom objects allows you to organize this information into groups, simplify your scripting, and reuse your scripts. For more information, see Using custom objects.
Movie clips are symbols that can play animation in a Flash movie. They are the only data type that refers to a graphical element. The movie clip data type allows you to control movie clip symbols using the methods of the MovieClip object. You call the methods using the dot (.) operator, as shown here:
myClip.startDrag(true); parentClip.childClip.getURL( "http://www.macromedia.com/support/" + product);