Loading different external text files when different value selected
I am having trouble trying to use actionscript to load different files when different values are changed. I am currently using a tilelist and they have different values so the code is something like this: (the title is just there, non-related)
if (startTileList.selectedItem.value == 1)
{
//textFile1 load here
txtTitle.text = "History";
}
else if (startTileList.selectedItem.value == 2)
{
//textFile2 load here
txtTit开发者_StackOverflow社区le.text = "Features";
}
else if (startTileList.selectedItem.value == 3)
{
//textFile3 load here
txtTitle.text = "Gallery";
}
So I want different text files to be loaded when different value is selected but I cannot seem to get it working. Anybody can give me any solutions? Much appreciated. Thanks in advance.
Here is a simple example for loading external text file:
var textField:TextField = new TextField();
//URLLoader used for loading an external file
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
//add to URLLoader a complete event listener (when the file is loaded)
urlLoader.addEventListener(Event.COMPLETE, loadingComplete);
//your application logic
var textURL : String;
if (true) {
textURL = "http://www.foo.com/text1.txt";
}else{
textURL = "http://www.foo.com/text2.txt";
}
//Tell to URLLoader to load the file
urlLoader.load(new URLRequest(textURL));
function loadingComplete(e:Event):void{
//remove the listener
urlLoader.removeEventListener(Event.COMPLETE, loadingComplete);
//update the text field with the loaded data
textField.text = urlLoader.data;
}
In this example, i use a URLLoader object. This is a native ActionScript3 object taht let you download external ressources. Loading an external ressource in AS3 is an asynchronous process, that's why you must listen to the COMPLETE event. Once loaded, you will find your data inside the property named 'data' of your URLLoader object.
精彩评论