RSS link icon

.

Read variables from text file with AS3


As you all might have noticed, lately I have converted to actionscript 3.0 instead of the old actionscripting, and yes as a lot of people say, its quite tough to convert, so I have to start from rock bottom. So here will be a lot of experimenting tutorials for flash actionscript 3.0. In this Flash tutorial you will see how to load variables in to flash from a text file.

As you might know a variable is a container, we define a container to keep simple information, it can be text, numbers, true/false etc. In this case we just want to load in simple text variables from a text file we call content.txt (remember to keep the content file in the same directory as the flash file).

First we will look at how the text file is build, it really simple you write everything in one line, separating variables with a & and starting with the variable name then = to its value like this.

var_1=first variable&var_2=second variable

If you now want to extract var_1 the result will be "first variable"

Now for the flash file, here is first what we want to set up.

This small example will only contain two dynamic text fields, so go open a new flash document, and drag out two text objects, go to properties and change their state from static to dynamic and give the text boxes an instance name (I called mine content_1 and content_2) Look at the properties in the image below.

read variable to flash

That was all we needed to do with the flash interface, of cause you can do a lot of visual stuff to make it look more interesting, but for this tutorial I will keep it simple and stick to the nerdy stuff.

As you know we are working with actionscript 3.0 so coding is done on the stage, so click somewhere on the stage, to deselect all, then go to the actionscript panel and type in the following code, lines with // are my comments on how the code works and what it does, you can delete them if you want.

var loader:URLLoader = new URLLoader();

//telling the loader that we are dealing with variables here.
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

//This is an eventlistener, these are used all the time in AS3
//learn to use them, this basically tells flash to listen to a specific event
//and then call a specific function.
//in Our case we listen for the even called COMPLETE which means it will active
//a function called "loading" when our flash movie has completed
loader.addEventListener(Event.COMPLETE, loading);

//Here we tell our loading which file to extract from.
loader.load(new URLRequest("content.txt"));

//This is the function that will happen when the eventlistener activates.
//basiclly it says that our text fields called content_1 and _2's text property
//should be equal to loader.data.var_1 and var_2 (as you might remember from the explanation above).
function loading (event:Event):void {
    content_1.text = loader.data.var_1
    content_2.text = loader.data.var_2
}

Admin Bob says: 2010-02-17

Hi Take, it look like a normal flash error caused by flash, when it cant find the file, try to place the text file in the same directory as the flash project and point to that.

best regards. Bob

Take says: 2010-02-16

Hi, I am resulted with this error, how can i fix this problem?

Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:///C:mydoc o copy rainingcs4 est.txt
 at cert_fla::MainTimeline/frame1()[C:mydocto copytrainingcs4as3main.as:16]

Admin Bob says: 2008-11-04

hi Marian, well you could selectt frame 10, give it an instance name in the property panel, then refere to the text box like this. frameName.textbox.text Hope it solved your problem

Marian says: 2008-11-02

Yes, but, how to do it, when dynamic text ( content_1.text) is in 10 frameQ

Rui says: 2008-08-14

Bob, what Michael wanted to say, i think, itChr(34)s if is possible to write variables with as3 to a txt

Admin Bob says: 2008-08-17

If that was his question, then yes, just set the variable equal to the txt.text object.

Admin Bob says: 2008-07-31

Hi Dennis, you might want so see how to make buttons functions, http://blog.0tutor.com/post.aspxid=110 then just put in the loader data in that button function.

Dennis says: 2008-07-25

Hi Bob, thank you for this article! Can you perhaps tell us how one can access the variables outside the loading function For instance to be able to load the text with a button

Admin Bob says: 2008-03-01

Michael, you must be thinking about something else, because variables are some of the most used in any programming languages, its simply a container to hold data, strings numbers, true false values etc. (and defined in actionscript by using var).

Michael says: 2008-03-01

Can you write variables with AS3. I really hope to achieve that as I know itChr(34)s only possible with AIR. But I am hoping that i was wrong

Admin Bob says: 2008-02-27

hi Marina, of cause you can, but the code is a bit different, and sorry I stoped using as2, but I bet you can find an example for it in as2 on the internet

Marina says: 2008-02-24

Can i make it in AS2