2011-01-19 53 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