【问题标题】:AS3 SWF not loadingAS3 SWF 未加载
【发布时间】:2025-11-21 22:25:01
【问题描述】:

我已经生成了一个加载和卸载 SWF 的类,但是,它似乎没有在视觉上加载到 swf,但它也没有返回任何错误。

父代码:

 //specify the path for the child SWF
  var PageURL:URLRequest = new URLRequest("home/home_index.swf");

//include the SWF loading class 
import MM_swfloader;
var mainPage:MM_swfloader = new MM_swfloader();
//evoke the load
mainPage.LoadSWF(PageURL);

MM_swfloader 类:

package  {

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;  
import flash.net.URLRequest;
/**
 * author: Me
 * email: lala@lala.com
 **/

public class MM_swfloader extends Sprite {

    public var loader:Loader = new Loader();
    public function MM_swfloader():void
     {
        // constructor code
     }

    public function LoadSWF(val:URLRequest):void{
        var loader:Loader = new Loader();
        loader.load(val);
        addChild(loader);
trace("loaded"); //returns true
    }

    public function UnLoadSWF():void {
        loader.unload();
    }

  }

} 

我不明白加载的 SWF 被加载到哪里。有什么想法吗?

【问题讨论】:

    标签: actionscript-3 actionscript flash loader


    【解决方案1】:

    您需要将您的 SWF 加载 classinstance 添加到父级 class 中的阶段:

    //specify the path for the child SWF
    var PageURL:URLRequest = new URLRequest("home/home_index.swf");
    
    //include the SWF loading class 
    import MM_swfloader;
    var mainPage:MM_swfloader = new MM_swfloader();
    // add the instance to the stage
    this.addChild(mainPage);
    //evoke the load
    mainPage.LoadSWF(PageURL);
    

    您的加载程序class 中的代码也存在一些问题,我已在下面修复(请参阅 cmets 以获得解释):

    // Poor practice to use the default namespace, stick it in com.lala 
    package  {
    
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;  
    import flash.net.URLRequest;
    /**
     * author: Me
     * email: lala@lala.com
     **/
    
    public class MM_swfloader extends Sprite {
    
        public var loader:Loader = new Loader();
    
        public function MM_swfloader():void
        {
            // constructor code
    
            // listen for loader complete on the loader instance
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
    
            // add loader to stage
            this.addChild(loader);
        }
    
        // Note that by convention loadSWF would be a better name for this method
        public function LoadSWF(val:URLRequest):void {
            // commented out to avoid creating a new locally-scoped
            // loader each time the method is called
            //var loader:Loader = new Loader(); 
    
            loader.load(val);
    
            // commented out to avoid adding child each time
            // method is called, added in constructor instead
            //addChild(loader);
    
            // loading is asynchronous, this needs to be in a handler
            // for the loader.complete event
            //trace("loaded"); //returns true
        }
    
        // call this unloadSWF
        public function UnLoadSWF():void {
            loader.unload();
        }
    
        // handler for loader.complete event
        private function loaderCompleteHandler(event:Event):void {
            trace("loaded");
        }   
      }
    } 
    

    【讨论】:

      【解决方案2】:

      您需要在完成时将加载程序内容添加到阶段,如下所示:

      import flash.net.URLRequest;
      import flash.display.Loader;
      import flash.events.Event;
      import flash.events.ProgressEvent;
      
      function startLoad()
      {
      var mLoader:Loader = new Loader();
      var mRequest:URLRequest = new URLRequest("someswf.swf");
      mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
      mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
      mLoader.load(mRequest);
      }
      
      function onCompleteHandler(loadEvent:Event)
      {
              addChild(loadEvent.currentTarget.content);
      }
      function onProgressHandler(mProgress:ProgressEvent)
      {
      var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
      trace(percent);
      }
      startLoad();
      

      【讨论】:

      • 虽然这不是一个类,但我需要从父级动态加载各种 SWF ......大约 20-30 左右。
      • 我把你的代码变成了一个可重用的类,但它没有渲染......我把所有东西都写到了addChild()。有什么想法吗?
      • 正如@net.uk.sweet 所说,您需要将 mainPage 实例添加到舞台。