
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
44
|
Chapter 2: Variables
Changing and Retrieving Variable Values
After we create a variable, we can assign and reassign its value as often as we like, as
shown in Example 2-1.
Notice that we changed the variable
x’s datatype from numeric to text data by sim-
ply assigning it a value of the desired type. Some programming languages don’t allow
the datatype of a variable to change, but ActionScript does.
Of course, creating variables and assigning values to them is useless if you can’t
retrieve the values later. To retrieve a variable’s value, simply use the variable’s name
wherever you want its value to be used. Anytime a variable’s name appears (except in
a declaration or on the left side of an assignment statement), the name is converted
to the variable’s value.
Here are some examples. Note that
_x is a built-in property representing a movie clip’s
horizontal position with no relation to the variable named
x used in preceding exam-
ples (see the “Variable Naming Styles” sidebar in Chapter 1, and see MovieClip._x in
the Language Reference).
newX = oldX + 5; // Set newX to the value of oldX plus 5
ball._x = newX; // Set the horizontal position of the
// ball movie clip to the value of newX
trace(firstName); // Display the value of firstName in the Output window
Note that in the expression ball._x ...