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.
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);
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
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;
}
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
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;
}
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.
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.