选择不同的值时,加载不同的外部文本文件
-
12-10-2019 - |
题
当更改不同的值时,我很难尝试使用ActionScript加载不同的文件。我目前正在使用tilelist,它们具有不同的值,因此代码就是这样:(标题就在那里,无关)
if (startTileList.selectedItem.value == 1)
{
//textFile1 load here
txtTitle.text = "History";
}
else if (startTileList.selectedItem.value == 2)
{
//textFile2 load here
txtTitle.text = "Features";
}
else if (startTileList.selectedItem.value == 3)
{
//textFile3 load here
txtTitle.text = "Gallery";
}
因此,我希望选择不同的值时加载不同的文本文件,但我似乎无法使其正常工作。有人可以给我任何解决方案吗?非常感激。提前致谢。
解决方案
这是加载外部文本文件的一个简单示例:
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;
}
在此示例中,我使用urlloader对象。这是一个本机actionscript3对象,让您下载外部ressources。在AS3中加载外部Ressource是一个异步过程,这就是为什么您必须收听完整事件的原因。加载后,您将在Urlloader对象的属性“数据”中找到数据。
不隶属于 StackOverflow