我正在創建一個2d flash遊戲(在flex/actionscript 3中編碼),在需要時下載資源。目前,我有它的設置是這樣的:運行時加載外部資產並在actionscript 3中重新使用預加載的資產?
AssetLoader.as
package
{
import flash.display.Loader;
import flash.net.URLRequest;
public class AssetLoader extends Loader
{
//set vars
private var url:String = "http://test.com/client/assets/";
public function AssetLoader(url:String)
{
Logger.log("AssetLoader request: " + this.url + url);
var request:URLRequest = new URLRequest(this.url + url);
this.load(request);
}
}
}
然後,在這裏我要加載的資產我做到以下幾點:
var asset:AssetLoader = new AssetLoader("ships/" + graphicId + ".gif");
asset.contentLoaderInfo.addEventListener(Event.COMPLETE, onShipAssetComplete, false, 0, true);
private function onShipAssetComplete(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
shipImage = Bitmap(loader.content);
shipImage.smoothing = true;
addChild(shipImage);
}
的事情是,這種方法不檢查已經下載的資產,所以它會在第二次請求同樣的資產時重新下載它們(我認爲)。
所以,我需要的是一個數組,其中存儲了所有下載的資產,並根據請求檢查此資產的名稱是否存在於數組中。所以如果它已經被下載,那麼來自內存的資源必須被返回而不是被重新下載。
我可以使assetloader一個靜態類,但我必須等待事件時,它的完成下載圖像火 - 所以我不能簡單地讓一個靜態函數返回相應的圖像。任何想法我應該如何做到這一點?
編輯徵求意見後企圖:
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public final class AssetManager
{
private static var assets:Object = {};
private static var preUrl:String = Settings.ASSETS_PRE_URL;
public static function load(postUrl:String):*
{
if (assets[postUrl])
{ //when the asset already exists
//continue
}
else
{ //the asset still has to be downloaded
var request:URLRequest = new URLRequest(preUrl + postUrl);
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
function(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
assets[postUrl] = loader.content;
}, false, 0, true);
}
}
}
}
EDIT2:另一種嘗試
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
public final class AssetManager
{
private static var assets:Object = {};
private static var preUrl:String = Settings.ASSETS_PRE_URL;
public static function load(postUrl:String):*
{
if (assets[postUrl])
{ //the asset already exists
var dispatcher:EventDispatcher = new EventDispatcher();
dispatcher.dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, assets[postUrl]));
}
else
{ //the asset still has to be downloaded
var request:URLRequest = new URLRequest(preUrl + postUrl);
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
function(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
assets[postUrl] = loader.content;
var dispatcher:EventDispatcher = new EventDispatcher();
dispatcher.dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, assets[postUrl]));
}, false, 0, true);
}
}
}
}
然後,我嘗試以下方法:
var asset:AssetManager = AssetManager.load("ships/" + graphicId + ".gif");
asset.addEventListener(CustomEvent.LOAD_COMPLETE, onShipAssetComplete, false, 0, true);
,但得到的錯誤,「undefin ed方法addEventListener通過類型靜態AssetManager的引用「(粗略翻譯)。
嘿,我不太清楚你想要我做什麼。我可以用資產URL和內容創建一個全球圖書館。當一個資產被請求時,我想我可以在圖書館已經找到這個URL的時候啓動它。但是,如何讓這個事件,然後返回此網址的庫內容?我不太明白。 – Tom 2009-08-11 23:02:51
我第一次嘗試編輯原始文章。但是,如評論中所解釋的,我陷入困境......也許您或其他人有任何建議? – Tom 2009-08-12 13:10:35
Lior可能暗示你有一張包含資源url - >加載資源的地圖。就像'assets [url] = loadedContent' – 2009-08-12 13:25:36