2.3 Changing and Retrieving Variable Values

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 2.  Variables

After we create a variable, we can 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 variable firstName firstName = "Graham";            // Set the value of firstName firstName = "Gillian";           // Change the value of firstName firstName = "Jessica";           // Change firstName again firstName = "James";             // Change firstName again var x = 10;                      // Declare x and assign it a numeric value x = "loading...please wait...";  // Assign x 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. 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 examples (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, ball is a movie clip's name, and ._x indicates its x-coordinate property (i.e., horizontal position on stage). We'll learn more about properties later. The last line, trace(firstName), displays a variable's value in the Output window while a script is running, which is handy for debugging your code.

2.3.1 Checking Whether a Variable Has a Value

Occasionally, we may wish to verify that a variable has been assigned a value before we make reference to it. As we learned earlier, a variable that has been declared but never assigned a value contains the special "nonvalue," undefined. To determine whether a variable has been assigned a value, we check if that variable's value belongs to the datatype undefined. For example:

if (typeof someVariable != "undefined") {   // Any code placed here is executed only if someVariable is not undefined }

Note the use of the inequality operator, !=, which determines whether two values are not equal, and the quotes around "undefined", because the typeof operator returns a string. In Flash MX, we can alternatively use the strict inequality operator (!= =) to check if a value is not equal to undefined or not of the type undefined.

if (someVariable !=  = undefined) {   // Any code placed here is executed only if someVariable is not undefined }

Both the typeof and the strict inequality techniques just shown prevent Flash from performing automatic datatype conversion when checking if someVariable is undefined. By contrast, due to automatic datatype conversion, the regular inequality operator (!=) checks not only whether someVariable is undefined, but also whether it contains the null value. For example:

if (someVariable != undefined) {   // Any code placed here is executed if someVariable is not undefined or null }

The null value is often used by programmers to indicate that a variable is intentionally empty, but still exists.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net