0

我已经生成了一个类来加载和卸载 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 被加载到哪里。有任何想法吗?

4

2 回答 2

1

您需要将instanceSWF 加载添加class到父级中的阶段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,我已在下面修复(请参阅注释以获取解释):

// 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");
    }   
  }
} 
于 2012-08-11T00:04:12.267 回答
0

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

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();
于 2012-08-10T19:43:31.647 回答