2012-08-10 101 views
0

我已經生成了一個類來加載和卸載SWF,但是,它似乎沒有加載swf可視化,但它也不會返回任何錯誤。AS3 SWF not loading

父代碼:

//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: [email protected] 
**/ 

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被載入。有任何想法嗎?

回答

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中的代碼也存在一些問題,我在下面解決了這個問題(請參閱註釋以獲得解釋):

// 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: [email protected] 
**/ 

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"); 
    } 
    } 
} 
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(); 
+0

雖然這不是一個類,但我需要從父類中隨時加載各種SWF ...約20-30。 – Neilisin 2012-08-10 20:35:31

+0

我把你的代碼變成了一個可重用類,它不會呈現......我把所有東西都交給了addChild()。有任何想法嗎? – Neilisin 2012-08-10 22:06:01

+0

As @ net.uk.sweet表示您需要將mainPage實例添加到舞臺。 – ezekielDFM 2012-08-11 02:03:19