Internet Cross Logo
Internet Cross your one stop web tutorial website

Loading data from text file dynamically

by Anand

You can load external data into flash. I will show you in this tutorial how to load data from a text file in to flash.

1.Open notepad to create a new text file and type &data1="This is the data" save it as loaddata.txt

2.Open a new movie in flash (note this is for flash mx 2004)

3.From the components panel drag and drop a text field.

4.Give the text field an instance name of loaded_txt

5.From the components panel drag and drop a button.

6.Go to the parameters of the button and give the label name as Load Data.

7.Give the button an instance name of load_btn

8.Create a new layer called actions and place this script in it.

var externalData:LoadVars = new LoadVars();

externalData.onLoad = function()
{
loaded_txt.text = externalData.data1;
}

load_btn.onPress = function()
{
externalData.load("loaddata.txt");
}

9.Now save the movie in the same folder as the text file.

10.Run the movie. Click on the button to see the loaded text.

11.I will explain how the code works

var externalData:LoadVars = new LoadVars();

Actionscript uses LoadVars object to load and store data that comes from a external file.

externalData.onLoad = function(){
loaded_txt.text = externalData.data1;
}
LoadVars has a property called onLoad this is called when a data is loaded from a external file. In our case when the data is loaded we are displaying the data in the text field loaded_txt

load_btn.onPress = function()
{
externalData.load("loaddata.txt");
}

When the button is pressed it fires the onPress event which executes all the command which are placed in it. In our case we are loading the externalData from the text file.

I hope this small tutorial was useful.


[Download File][Page 1]
Visit our forums to discuss / post your problems.