使用裝載程式載入外部影象 SWF
-
建立一個 Loader 物件:
var loader:Loader = new Loader(); //import
-
在載入器上新增偵聽器。標準的是完整的和 io /安全性錯誤
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); //when the loader is done loading, call this function loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadIOError); //if the file isn't found loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadSecurityError); //if the file isn't allowed to be loaded
-
載入所需的檔案:
loader.load(new URLRequest("image.png"));
-
建立事件處理程式:
function loadComplete(e:Event):void { //load complete //the loader is actually a display object itself, so you can just add it to the display list addChild(loader) //or addChild(loader.content) to add the root content of what was loaded; } function loadIOError(e:IOErrorEvent):void { //the file failed to load, } function loadSecurityError(e:SecurityError):void { //the file wasn't allowed to load }
使用 Loader 類載入是非同步的。這意味著在呼叫
loader.load
後,應用程式將在檔案載入時繼續執行。在載入程式排程Event.COMPLETE
事件之前,你的載入程式內容不可用。
需要匯入:
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;