简体   繁体   中英

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.

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:

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. Any ideas?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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