How to Import a VAR in ActionScript 3

You use a VAR, also known as a variable in ActionScript 3, to declare a variable that can store numbers, text, images, objects and arrays of numbers and letters. Importing text or number variables into your Flash AS3 program requires you to import variable name and variable data value pairs from an external file on your Web server or in your computer directory. Once the ActionScript code reads these variables, they can display on the Flash stage or data processing calculations in your ActionScript program can use them.

Things You'll Need

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

Instructions

    • 1

      Start the Flash program. Click "Flash File(ActionScript 3.0)" from the splash window to create a new file for an ActionScript 3 Flash animation project.

    • 2

      Click on the "Actions" option from the Windows menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button, and type in the code below to create and place an AS3 TextField named "tf" that positions at Flash stage coordinates "10,10" and that has a width or 500 pixels and a height of 380 pixels.

      var tf:TextField = new TextField();

      tf.x = 10;

      tf.y = 10;

      tf.width = 500;

      tf.height = 380;

      addChild(tf);

    • 3

      Type the code below starting at the next line in the Actions editor to create a Loader object named "myTextLoader" that will retrieve and load a text file named "first.txt" that you format with the MIME file format required for variables.

      var myTextLoader:URLLoader = new URLLoader();

      var textFile:URLRequest = new URLRequest("first.txt")

      myTextLoader.load(textFile);

      myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES

    • 4

      Type the code below starting at the next line in the Actions editor to add an event listener to the "myTextLoader" Loader object that will detect when the first.txt file completes loading and performs the completeHandler function which will place the contents of the variable named JohnScore from the first.txt text file into the "tf" TextField and display its contents.

      myTextLoader.addEventListener(Event.COMPLETE, completeHandler);

      function completeHandler(event:Event):void {

      tf.text = myTextLoader.data.JohnScore;

      }

    • 5

      Open a text editor and type, or copy and paste, the variables and values in standard variable MIME format as shown below. Save the file as a text file named "first.txt" in the same directory in which you plan to save your Flash project file.

      JohnScore=John's present score is 100&DavidScore=David's present score is 300&AmyScore=Amy's present score is 5000

    • 6

      Copy and paste the code below into the Actions editor to ensure that you have no syntax errors and that the import variable program runs correctly.

      var tf:TextField = new TextField();

      tf.x = 10;

      tf.y = 10;

      tf.width = 500;

      tf.height = 380;

      addChild(tf);

      var myTextLoader:URLLoader = new URLLoader();

      var textFile:URLRequest = new URLRequest("first.txt")

      myTextLoader.load(textFile);

      myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES

      myTextLoader.addEventListener(Event.COMPLETE, completeHandler);

      function completeHandler(event:Event):void {

      tf.text = myTextLoader.data.JohnScore;

      }

    • 7

      Click "File" on the main Flash menu bar and select "Save." Save the Flash file in the same directory in which you saved the "first.txt" file.

    • 8

      Click the "Control" option on the main Flash menu bar and select "TestMovie." Observe that this places the contents of the JohnScore variable, "John's present score is 100," in the TextField you instructed Flash to create and place on the stage.

Learnify Hub © www.0685.com All Rights Reserved