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 variablefirstNamefirstName = "Graham"; // Set the value offirstNamefirstName = "Gillian"; // Change the value offirstNamefirstName = "Jessica"; // ChangefirstNameagain firstName = "James"; // ChangefirstNameagain var x = 10; // Declarexand assign a numeric value x = "loading...please wait..."; // Assignxa 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; // SetnewXto the value ofoldXplus 5 ball._x = newX; // Set the horizontal position of the //ballmovie clip to the value ofnewXtrace(firstName); // Display the value offirstNamein 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access