2013-07-31 55 views
1

我想加載外部子swf到父swf(在同一目錄下)。 我確實嘗試過,孩子被加載,但其中的班級沒有工作。如何加載外部swf和使用子類

1)如何加載兒童swf的類?

2)我怎樣才能卸載類子SWF的?(becoz可能有許多分機的SWF不同類)

THX

main.fla

function startLoad(){ 
    var mLoader:Loader = new Loader(); 
    var mRequest:URLRequest = new URLRequest("game1.swf"); 
    var mLoaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain); 
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); 
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler); 
    mLoader.load(mRequest, mLoaderContext); 
} 

function onCompleteHandler(loadEvent:Event){ 
    var keyManager:Class = ApplicationDomain.currentDomain.getDefinition("net.keithhair.KeyManager") as Class; 
    addChild(loadEvent.currentTarget.content); 
} 

的Game1。 FLA

import net.keithhair.KeyManager; 

    keyManager=new KeyManager(stage); 
    keyManager.addKey(["a"], doSomething); 
    function doSomething():void { 
    //do something 
    } 

結果:

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at net.keithhair::KeyManager/removeListeners() 
at net.keithhair::KeyManager/addListeners() 
at net.keithhair::KeyManager() 
at game1_fla::MainTimeline/frame1() 
+0

你試過嗎? http://stackoverflow.com/questions/1634757/as3-instantiate-class-from-external-swf –

+0

試過。相同的結果:TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t在net.keithhair ::的KeyManager/removeListeners() \t在net.keithhair ::的KeyManager/addListeners() \t在net.keithhair ::的KeyManager() \t在main_fla :: MainTimeline/fooLoadComplete() – frankyG

回答

0
function Constructor(){ 
    if (stage){ 
     onAddedToStage(); 
    } else { 
     addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
    } 
} 

function onAddedToStage(evt:Event=null):void { 
    var keyManager:KeyManager; 
    trace("here!") 
    keyManager=new KeyManager(stage); 
    keyManager.addKey(["a"], doSomething 
} 
Constructor(); 

上面的代碼解決我的所有問題。 thank you very much destinier & senocular