Changing and Retrieving Variable Values
After we’ve created a variable, we may assign and reassign its value as often as we like, as shown in Example 2.1.
Example 2-1. Changing Variable Values
var firstName; // Declare the variablefirstName
firstName = "Graham"; // Set the value offirstName
firstName = "Gillian"; // Change the value offirstName
firstName = "Jessica"; // ChangefirstName
again firstName = "James"; // ChangefirstName
again var x = 10; // Declarex
and assign a numeric value x = "loading...please wait..."; // Assignx
a text value
Notice that we changed the variable x
’s
datatype from numeric to text data by simply
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:
newX = oldX + 5; // SetnewX
to the value ofoldX
plus 5 ball._x = newX; // Set the horizontal position of the //ball
movie clip to the value ofnewX
trace(firstName); // Display the value offirstName
in the Output window
Note that in the expression ball._x
,
ball
is a movie clip’s name, and the
._x
indicates its x-coordinate property ...
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.