2011-01-19 20 views
0

我在尝试使用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"; 
} 

所以我希望在不同的价值是不同的文本文件被加载选择,但我似乎无法得到它的工作。任何人都可以给我任何解决方案?非常感激。提前致谢。

回答

0

下面是用于加载外部文本文件的简单示例:

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对象,可让您下载外部资源。 在AS3中加载外部资源是一个异步过程,因此您必须监听COMPLETE事件。 加载后,您会在URLLoader对象的名为'data'的属性中找到您的数据。

+0

请在代码中添加注释。只是发布解决方案并不能解释事情。 – weltraumpirat 2011-01-20 07:28:51