使用装载程序加载外部图像 SWF

  1. 创建一个 Loader 对象:

    var loader:Loader = new Loader();   //import 
    
  2. 在加载器上添加侦听器。标准的是完整的和 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
    
  3. 加载所需的文件:

     loader.load(new URLRequest("image.png"));
    
  4. 创建事件处理程序:

     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;