2011-05-10 57 views
0

早上好,Actionscript 3如何基於Web服務值將PNG加載到舞臺上?

我在查詢weather.com XML Web服務以獲取當前溫度,感覺像是臨時和當前狀態圖標。我有前兩個值在舞臺上分開文本字段,這很好。但是,我正在嘗試實現一個switch語句,以便通過腳本讀取XML中的當前條件圖標值,並將相應的PNG變爲UILoader源值。

首先,UILoader是用於在舞臺上顯示PNG的最佳組件嗎?

這裏是我的代碼迄今:

var src: UILoader.source=uicondicon.source; 
switch(src){ 
case "0 PNG":     
src=trace(resultXML.cc.icon)=0; 
break; 
case "11 PNG": 
src=trace(resultXML.cc.icon)=11; 
break; 
default src=trace(resultXML.cc.icon)=0: 
} 

當我編譯上面的代碼,我收到錯誤1084期待COLONG SRC之前的代碼(我的默認分配)的最後一行。

鑑於可能需要顯示47個不同的圖標,這是實現此功能的最佳方式嗎?

最後,我如何才能使UILoader組件僅在應用程序中的一組幀中顯示?

非常感謝!

回答

2

1)你得到的錯誤可能是因爲您的switch語句中的最後一行用冒號而不是分號結束 - 「默認SRC =跟蹤(resultXML.cc.icon)= 0:」

2)我從來沒有實際使用的UILoader - 我通常只是用裝載機:

var loader:Loader = new Loader; 
var images:Array = []; 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler); 

loader.load(new URLRequest("image url here.jpg")); 

protected function completeHandler(e:Event):void { 
// you can pull out the loader content and cast it as a bitmap... 
images.push(loader.content as Bitmap); 

// or you can simply add the loader to the stage 
addChild(loader); 
clearListeners(); 

} 

protected function errorHandler(e:IOErrorEvent):void { 
trace("Error loading image! Here's the error:\n" + e); 
clearListeners(); 
} 

protected function progressHandler(e:ProgressEvent):void { 
trace("Load is " + (100 * e.bytesLoaded/e.byteTotal) + " percent complete..."); 
} 

protected function clearListeners():void { 
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); 
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler); 
} 

3)如果你在一次加載多張圖片,你應該知道,在你的負載調用的順序無關按照它們完成的順序執行。所以你不能假設,因爲你加載圖像A,然後圖像B,圖像A將首先完成。所以,你必須做一些邏輯,讓您的裝載機的軌道 - 例如,你可以有這樣的設置:

var urls:Array = [this array has all of your urls in it]; 
var loaders:Array = []; 
var images:Array = []; 

protected function initLoaders():void { 
for (var i:int = 0; i < urls.length; i++) { 
    var loader:Loader = new Loader(); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
    // you'll want to add your other handlers here too for IOErrorEvent and Progress. 
    loader.load(new URLRequest(urls[i])); 
    loaders[i] = loader; 
} 
} 

protected function completeHandler(e:Event):void { 
var loaderIndex:int = loaders.indexOf(e.currentTarget); // this tells you which loader finished 
images[loaderIndex] = e.currentTarget.content as Bitmap; // I think this will work, you may have to play with Array syntax to make sure you're not adding tons of empty items and screwing up your order. 

if (images.length == urls.length) { 
    trace("All images loaded!"); 
} 
} 

4)最後,不要使用框架。你想在外部類中編寫你的代碼。如果必須綁定到給定幀,則根據當前幀使用visible = true或visible = false。但是,所有這些都可以並且應該在代碼中處理,因爲這是真正的業務邏輯,不應該與您的視圖(即與幀相關的視覺顯示)緊密耦合。

希望有幫助!

+0

非常感謝您的建議。我正在根據trace(resultXML.cc.icon)的值一次添加一個圖像。所以,如果trace(resultXML.cc.icon)= 11,那麼應該在舞臺上加載11.png,如果trace(resultXML.cc.icon)= 12,那麼12.png應該加載到舞臺上等等...... – SidC 2011-05-10 16:37:42

+0

I會使用類似我所描述的機制來提前加載所有圖像,並在其上粘貼預加載器。然後,一旦你加載了所有的圖片,只需要addChild(圖片[index]),無論你想要顯示哪一個。此外,您可以查看Greensock的「LoaderMax」,這是一個旨在幫助您無縫加載大量項目的自定義框架。乾杯! – Myk 2011-05-10 19:29:48

相關問題