Using variables in an application

In this section, you use variables in short code snippets of ActionScript. You need to declare and initialize a variable in a script before you can use it in an expression. Expressions are combinations of operands and operators that represent a value. For example, in the expression i+2, i and 2 are operands, and + is an operator.

If you do not initialize a variable before you use it in an expression, the variable is undefined and may cause unexpected results. For more information on writing expressions, see Syntax and Language Fundamentals.

If you use an undefined variable, as shown in the following example, the variable's value in Flash Player 7 and later will be NaN, and your script might produce unintended results:

var squared:Number = myNum * myNum;
trace(squared); // NaN 
var myNum:Number = 6;

In the following example, the statement that declares and initializes the variable myNum comes first, so squared can be replaced with a value:

var myNum:Number = 6;
var squared:Number = myNum * myNum;
trace(squared); // 36

Similar behavior occurs when you pass an undefined variable to a method or function, as shown next.

To compare undefined and defined variables being passed to a function:

  1. Drag a Button component to the Stage from the Components panel.
  2. Open the Property inspector and type bad_button into the Instance Name text box.
  3. Type the following code on Frame 1 of the Timeline.
    // Does not work
    function badClickListener(evt:Object):Void {
        getURL(targetUrl);
        var targetUrl:String = "http://www.adobe.com";
    }
    bad_button.addEventListener("click", badClickListener);
    
  4. Select Control > Test Movie, and notice that the button does not work (it doesn't open the web page).
  5. Drag another Button component onto the Stage. Select the button.
  6. Open the Property inspector, and type good_button into the Instance Name text box.
  7. Add the following ActionScript to Frame 1 of the Timeline (following the previous ActionScript you added):
    // Works
    function goodClickListener(evt:Object):Void {
        var targetUrl:String = "http://www.adobe.com";
        getURL(targetUrl);
    }
    good_button.addEventListener("click", goodClickListener);
    
  8. Select Control > Test Movie and click the second button you added to the Stage.

    This button properly opens the web page.

The type of data that a variable contains affects how and when the variable's value changes. Primitive data types, such as strings and numbers, are passed by value, which means the current value of the variable is used rather than a reference to that value. Examples of complex data types include the Array and Object data types.

In the following example, you set myNum to 15 and copy the value into otherNum. When you change myNum to 30 (in line 3 of the code), the value of otherNum remains 15 because otherNum doesn't look to myNum for its value. The otherNum variable contains the value of myNum that it receives (in line 2 of the code).

To use variables in your ActionScript:

  1. Create a new Flash document, and save it as var_example.fla.
  2. Select Frame 1 of the Timeline, and type the following code into the Actions panel:
    var myNum:Number = 15;
    var otherNum:Number = myNum;
    myNum = 30;
    trace(myNum); // 30
    trace(otherNum); // 15
    

    When you change myNum to 30 (in line 3 of the code), the value of otherNum remains 15 because otherNum doesn't look to myNum for its value. The otherNum variable contains the value of myNum that it receives (in line 2 of the code).

  3. Select Control > Test Movie to see the values display in the Output panel.
  4. Now add the following ActionScript after the code you added in step 2:
    function sqr(myNum:Number):Number {
        myNum *= myNum;
        return myNum;
    }
    var inValue:Number = 3;
    var outValue:Number = sqr(inValue);
    trace(inValue); // 3
    trace(outValue); // 9
    

    In the this code, the variable inValue contains a primitive value, 3, so the value passes to the sqr() function, and the returned value is 9. The value of the variable inValue does not change, although the value of myNum in the function changes.

  5. Select Control > Test Movie to see the values display in the Output panel.

The Object data type can contain such a large amount of complex information that a variable with this type doesn't hold an actual value; it holds a reference to a value. This reference is similar to an alias that points to the contents of the variable. When the variable needs to know its value, the reference asks for the contents and returns the answer without transferring the value to the variable.

For information on passing a variable by reference, see Passing a variable by reference.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000660.html