Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Load External Text Database File to Array with Flash ActionScript 3

This Flash ActionScript tutorial shows how to load an external text database file to an Array and how to manipulate the Array.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Most Flash Developers sometimes use external text database to store data. The text file will usually load to an Array and the data can be accessed easily. This Flash ActionScript tutorial shows you how to load an external text database file into an Array.

The Flash Movie of this tutorial is shown as above.

Flash ActionScript Codes:

//Create a loader with the name myLoader
//This loader will be used to load the text file
var myLoader:URLLoader = new URLLoader();

 

//Load the text file into myLoader
//This text file has eight lines
//This text file has the format:
/*
Slang 1|Slang 1 example sentence
Slang 2|Slang 2 example sentence
Slang 3|Slang 3 example sentence
Slang 4|Slang 4 example sentence
Slang 5|Slang 5 example sentence
Slang 6|Slang 6 example sentence
Slang 7|Slang 7 example sentence
Slang 8|Slang 8 example sentence
*/
myLoader.load(new URLRequest("slang.txt"));

 

//Listen when the loading of the text file COMPLETE
//The function slangLoaded will be called when the loading COMPLETE
myLoader.addEventListener(Event.COMPLETE, slangLoaded);

 

//Declare some variables to use later
var slangArray:Array = new Array();
var slangContent:String
var slangSplit:String;

 

function slangLoaded(event:Event):void {

//Hold the text file as a string
//slangContent is now in the format:
/*
Slang 1|Slang 1 example sentence

Slang 2|Slang 2 example sentence

Slang 3|Slang 3 example sentence

Slang 4|Slang 4 example sentence

Slang 5|Slang 5 example sentence

Slang 6|Slang 6 example sentence

Slang 7|Slang 7 example sentence

Slang 8|Slang 8 example sentence
*/
slangContent = event.target.data;

 

//Split the slangContent string into an Array
//The size of this Array is 8 since there are total 8 slangs
// \r is the return
// \n is the line feed
slangArray = slangContent.split("\r\n");

 

//slangArray is now an Array
//You can play around whatever you like, for example:
//slangArray.length is the total slang
//slangArray[0] is the first slang
//slangArray[slangArray.length -1] is the last slang
totalSlang_txt.text = "There are total " + slangArray.length + " slangs in our database";

 

//Let's pick a random Slang from slangArray
//Your visitors may see a different slang everytime they access this page
slangSplit = slangArray[Math.floor(Math.random() * slangArray.length)];

 

//Split the result and join by two carriage return
//1st step: Split
//Remember that each slang was seperated by "|"
//2nd step: Join
//We wish a blank line between the Slang and Slang example sentence
slangSplit = slangSplit.split("|").join("\r\r");

 

//Display the Slang today!
output_txt.text = slangSplit;

}

Download Flash Source File:

Flash Source File array-7.fla

Text Database File: slang.txt

Remarks:

This Flash ActionScript tutorial shows how to load an external text database file into an Array.