Assigning Variables
Now comes the fun part—putting some data into our variables. If you’re still playing along with the bank analogy, this is the “deposit money into our account” step. To assign a variable a value, we use:
variableName
=value
;
where variableName
is the name of a
variable, and value
is the data
we’re assigning to that variable. Here’s an applied
example:
bookTitle = "ActionScript: The Definitive Guide";
On the left side of the equal sign, the word
bookTitle
is the variable’s
name (its identifier). On
the right side of the equal sign, the phrase “ActionScript: The
Definitive Guide” is the variable’s value
—the datum you’re depositing. The equal sign
itself is called the assignment operator. It
tells Flash that you want to assign (i.e., deposit) whatever is on
the right of the equal sign to the variable shown on the left. If the
variable on the left doesn’t exist yet, Flash creates it
(though relying on the interpreter to implicitly create variables
isn’t recommended).
Here are two more variable assignment examples:
speed = 25; output = "thank you";
The first example assigns the integer 25 to the variable
speed
, showing that variables can contain numbers
as well as text. We’ll see shortly that they can contain other
kinds of data as well. The second example assigns the text
“thank you” to the variable output
.
Notice that we use straight double quotation marks (” “) to delimit a
text string in ActionScript.
Now let’s look at a slightly more complicated example that assigns ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.