O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loading Variables from a Text File

Problem

You want to load variables from an external text file into your Flash movie.

Solution

Use the URLLoader.load() method in conjunction with DataFormat.VARIABLES as the dataFormat to read URL-encoded data into a Flash movie.

Discussion

You should use the URLLoader.load() method whenever you want to load URL-encoded data into your Flash movie from a text file. This technique allows your Flash movie to access values that frequently change without modifying the Flash movie itself.

The load() method requires a URLRequest instance as a parameter that points to the URL of the text file. The URL can be an absolute or relative address. Additionally, the URLLoader needs to be configured to interpret the text as URL-encoded variables instead of plain text. Setting the dataFormat property of the URLLoader instance to the DataFormat.VARIABLES constant accomplishes this. Here is code to illustrate loading data from a text file:

import flash.net.*;

// You must first create the URLLoader object.
var example:URLLoader = new URLLoader();

// Configure the instance to interpret data as URL-encoded variables
example.dataFormat = DataFormat.VARIABLES;

// This example loads values from a text file at an absolute URL.
example.load( new URLRequest( "http://www.darronschall.com/example.txt" ) );
  
// This example loads values from a text file located at a relative 
// URL, in this case, in the same directory as the .swf file. example.load( new URLRequest( "example.txt" ) ); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required