
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
42
|
Chapter 2: Variables
We can give variables an initial value at the same time we create them, as follows:
var ballSpeed = 5; // Velocity of ball, default 5, max 10
var score = 0; // Player's current score
var hiScore = 0; // High score (not saved between sessions)
var player1 = "1P"; // Player's name defaults to 1P
For even tidier variable management, object-oriented programmers will want to store
all variables within a class, as discussed in Chapter 12.
Assigning Values to Variables
Now comes the fun part—putting some data into our variables. If you’re still play-
ing along with the bank analogy, this is the “deposit money into our account” step.
To assign a value to a variable, 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 example:
bookTitle = "ActionScript for Flash MX: The Definitive Guide";
On the left side of the equals sign, the word bookTitle is the variable’s name (its iden-
tifier). On the right side of the equals sign, the phrase “ActionScript for Flash MX:
The Definitive Guide” is the variable’s value—the datum you’re depositing. The
equals sign is called the assignment operator. It tells Flash that you want to assign (i.
e., deposit) whatever is on the right of the equals sign