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.
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.
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.
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
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.
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
- }
sumber : http://blog.0tutor.com/post.aspx?id=110
Anda baru saja membaca artikel yang berkategori flashKeren
dengan judul Read variables from text file with AS3. Anda bisa bookmark halaman ini dengan URL http://bahasaflash.blogspot.com/2013/01/rss-link-icon-javascript-illustrator.html. Terima kasih!
Ditulis oleh:
Unknown - Rabu, 02 Januari 2013

Belum ada komentar untuk "Read variables from text file with AS3"
Posting Komentar