Groups | Blog | Home
all groups > flash data integration > september 2005 >

flash data integration : Loading .txt into flash


brentam
9/8/2005 10:26:18 PM
I hope this is simple, but can't figure out how to build.

I have a text file that has (7) separate sentences "qoutes." I am wanting
flash to load only one qoute from the text file per page view. For example,
each page view would generate a random one line quote from the txt file. This
would be great if it could load randomly.

Is this an actionscript function? I have built a random photo loader, but have
not worked with .txt files.

Any suggestions or help would be much appreciated.
SimonTheSwift
9/10/2005 1:09:29 PM
Hello brentam,

all right, first a bit of an overview - accessing external data in Flash is a
topic on its own. There are several ways:

- using the LoadVars class
- using the XML class
- using server-side scripts (PHP, ASP, ColdFusion)
- making use of Data Components (Flash Professional MX 2004)

To understand why Flash is the way it is with external data access, try
reading through the Macromedia Flash Player Client-Side Security white paper,
available as a .pdf file on MM's site.

Now, let's get to your case. I think that the easiest way to do it is by using
the LoadVars class. I will give you a simple example. To learn more about it,
please, consult the ActionScript Reference Guide, also available as a .pdf (see
chapter called LoadVar class).

1. Create a new .txt file and call it "quotes.txt"

2. Copy the following into it:

quotes=hello,john brown,do not smoke,it will kill you,and it bothers those
around you

3. Start a new Flash document. In the Actions window write the following code:

var extVars:LoadVars = new LoadVars();
extVars.load("quotes.txt");
extVars.onLoad = function() {
var myQuotes:Array = new Array();
myQuotes = extVars.quotes.toString().split(",");
trace(myQuotes);
}

Make sure to save this file in the same directory where you quotes.txt file is.

Run the file by pressing Ctrl+Enter. In the Output window, you will see the
loaded variables (try using Ctrl+Alt+V to view the structure of variables used
in your movie). I used myQuotes as an array, to make it easier for a random
access.

You can also use a different structure in your .txt file. Look at this:

quote01=hello&quote02=john brown&quote03=do not smoke&quote04=it will kill
you&quote05=and it bothers those around you

Now you have five variables stored in the .txt file, instead of one as in the
first version. Experiment with it to see what's to suit you better. Do not
forget that to access the quotes in Flash, you will use extVars.quote01 ...
extVars.quote05 and you won't have to use the array.


Well, I hope this will help sovling your problem. If you have any questions,
let us know.

Good luck, Simon.
AddThis Social Bookmark Button