How to Use VAR in AS3

A variable (VAR) in AS3, the Flash ActionScript 3 programming language, is used to define numeric and character variables. These VARs are fundamental to performing math calculations for video game programming, storing text messages for display on the Flash stage, and creating graphic objects on the Flash stage. There are several VAR object types you can declare (or instantiate) with the AS3 VAR class. These include Boolean, int, Number, String and uint for alphanumeric variables and MovieClip, Sprite, Shape and TextField for graphic and text container objects.

Things You'll Need

  • Adobe Flash Professional: CS3, CS4 or CS5 versions
Show More

Instructions

    • 1

      Instantiate (declare) a VAR using the general AS3 syntax form:

      va <instance name of var>:<type of var> = new <type of var>();

      In this case, "instance name of var" is a name you assign for the variable and "type of var" is the class name of the type of variable object you want to create a variable for.

    • 2

      Use the following var statement:

      var A:Number = new Number();

      to instantiate a number variable named A which can be used to store decimal numbers.

      Assign the number variable A a decimal value of 5.99 and print the results to the AS3 output panel with the code

      A = 5.99;

      trace (A);

      Declare another number variable, called B. Store the decimal number 0.01 in it and add it the value of variable A, 5.99, then display the result in the Output panel, 6.00 with the code

      var B:Number = new Number();

      B = 0.01;

      trace (A+B);

    • 3

      Use the var statement

      var B:String= new String();

      B = "this is text stored in string variable named B"

      to instantiate a string variable named B which is used to store text in quotes above. Always surround text with double quotes when assigning text to a string variable.

    • 4

      Use the var statement

      var myButton:MovieClip = new MovieClip();

      to instantiate a movie clip variable named myButton which is used to store movie clips and build new movie clips. Add a red rectangle graphic to the myButton MovieClip variable with graphic methods and properties of the MovieClip class with the code

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(100, 50, 50, 20);

      myButton.graphics.endFill();

      Place myButton movie clip variable object on the stage with the AS3 addChild method code

      addChild(myButton);

    • 5

      Use the var statement

      var textdisplay:TextField = new TextField(");

      to instantiate a TextField variable named textdisplay. Assign text to be stored in the textdisplay variable with the TextField "text" property as in

      textDisplay.text = "this is the text in the textDisplay TextField";

      Always include double quotation marks around the text to be stored in the text property of a TextField object.

Learnify Hub © www.0685.com All Rights Reserved