简体   繁体   English

未加载AS3 SWF

[英]AS3 SWF not loading

I've generated a class to load and unload SWFs, however, it does not seem to load to swf visually, but it doesn't return any errors either. 我已经生成了一个用于加载和卸载SWF的类,但是,它似乎并没有在视觉上加载到SWF中,但是它也不返回任何错误。

Parent code: 父代码:

 //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 class: 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();
    }

  }

} 

I don't understand where the loaded SWF is being loaded to. 我不了解已加载的SWF加载到的位置。 Any ideas? 有任何想法吗?

You need to add the instance of your SWF loading class to the stage in the parent class : 您需要将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);

There are also a couple of problems with the code in your loader class which I have fixed below (see the comments for an explanation): 加载程序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");
    }   
  }
} 

You need to add the loader content to the stage on complete, like so: 您需要完整地将加载器内容添加到场景中,如下所示:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM