0
ActionScript似乎無法支持同步載入一個PNG。我嘗試過異步加載它,但是接着出現讓你的函數等待加載完成的問題。我無法做到這一點。我曾嘗試在while循環中調用setInterval和setTimout,並在加載完成時中斷,但我得到了一些非常神祕的行爲。對於函數參數,我傳遞了一個名爲doNothing的函數,它只是一個空函數。當我運行時,setInterval或setTimeout會一遍又一遍,很快。他們不再等待再次打電話之前提供的時間。另外,沒有任何東西永遠不會被呼叫。如何在ActionScript 3的函數調用中加載並返回.png文件?
這裏是我的代碼:
private function getData(extension:String):Bitmap
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onComplete);
loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onComplete);
loader.load(new URLRequest(baseDir + extension));
while (bitmap == null)
{
var test:Number = setInterval(doNothing, 1000);
}
var ret:Bitmap = bitmap;
bitmap = null;
return ret;
}
function onComplete(event:Event):void
{
bitmap = Bitmap(LoaderInfo(event.target).content);
}
function doNothing():void
{
trace("did nothing");
}
文件加載協議應該有一個您可以綁定的事件,以便您在完全加載時收到通知。你在做什麼?請輸入密碼? – TheZ 2012-07-06 19:09:20
由於Flex使用單線程模型,試圖實現類似的東西會導致界面在圖像加載過程中凍結 因此,調用getData函數的代碼應該監聽,而不是直接期待數據。像TheZ說的那樣,你想要做什麼? – cporte 2012-07-06 19:42:53
是的,這實際上是我需要的。 – user1507613 2012-07-06 19:44:34