External Variables

I l @ ve RuBoard

External Variables

Flash allows you to bring external text into a movie by using ActionScript. You can put the external text in the HTML page or in a completely separate file.

Variables from the HTML Page

You can define the initial value of a variable in the HTML page where the Flash movie is embedded. Why would you want to do this? Well, consider an example like a Flash banner that appears on top of a Web page. Instead of making 30 banners for 30 Web pages, you could make one banner and have the different text in the HTML of each page so that the banner displays a different title on each page. One Flash movie works for all 30 pages.

To get some text, or any variable value, from the HTML page, you need to add it to the movie source tags in the HTML. So, first, publish your movie. Then, open the HTML page created. You'll see the combination OBJECT/EMBED tag there.

The OBJECT tag defines the Flash movie for the Internet Explorer browser on Windows machines. The EMBED tag defines it for other browsers, so you'll have to make the change in two places. For instance, if you want to set the variable textField , you need to do this in the OBJECT tag:

 <PARAM NAME=movie VALUE="10banner.swf?textField=Text from HTML!"> 

Then, later in the EMBED tag, you need to make a similar change:

 src="10banner.swf?textField=Text from HTML!" 

Check out the file 10banner.html in a text editor such as SimpleText or WordPad to see exactly where the changes go.

The result is that when the movie loads, the variable is created and set to this value. Check the movie 10banner.fla to see a movie that is set up to use this type of thing. Drag and drop the 10banner.html file onto your favorite browser to see what happens.

You can set more than one variable by placing a & character after each variable declaration and then starting a new declaration. Here is an example:

 <PARAM NAME=movie VALUE="10banner.swf?textField=Text from HTML!&myNum=7"> 

Variables from External Files

You can also get variables from an external text file. Why would you want to do this? Well, suppose that you make a movie that displays some dynamic information, such as a weather report. You can make the movie once and then change a text file on the server to update the text. Someone who doesn't even use Flash can then update the contents of the movie.

To do this, you'll need a single command inside the Flash movie. The file 10external.fla has an example. It loads the variables from the file 10external.txt.

 loadVariables ("10external.txt",_root); 

If you run the movie, the text in the dynamic field textField is replaced with text from the text file. Here are the contents of the text file:

 textField=Weather today: Sunny, with a high of 72. Chance of showers tonight. 

An external text file works the same as the extra information in the HTML tag. You can define as many variables as you want, separated by & . Line breaks don't matter, which is why all three lines of text get assigned to the textField variable.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net