2012-08-10 143 views
0

溝通,我在一個項目中,如果母公司是從影片剪輯中調用一個孩子從幼兒到父SWF通信掙扎。從兒童SWF

父影片剪輯有這樣的行動層

var loader:Loader = new Loader(); 
var defaultSWF:URLRequest = new URLRequest("child.swf"); 
loader.load(defaultSWF); 

//I'm using an if statement to check if the movieclip is loaded to stage 

if (loader.parent != null) { 
loader.dispatchEvent(new Event(Event.ADDED_TO_STAGE)); 
trace("loader added to stage"); // trace returns positive 
loader.content.addEventListener("directions", childLoader_someSignalHandler); 
} 

//once a button in the child SWF is clicks the playhead will 
//change position on the parent.  
function childLoader_someSignalHandler(event:Event):void { 
    gotoAndStop("directions"); 
} 

孩子SWF已驗證碼發送到父:

centre_btn.addEventListener(MouseEvent.MOUSE_DOWN, childBtn_mouseDownHandler); 
// thanks to "Florent" for this code. 
function childBtn_mouseDownHandler(event:MouseEvent) { 
    dispatchEvent(new Event("directions")); 
} 

我返回我的錯誤是:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

+0

從您的示例中,不清楚哪一行代碼導致此錯誤。當在子SWF中單擊該按鈕時,childLoader_someSignalHandler()中的代碼是否被執行? – 2012-08-10 16:06:01

+0

答案在於下面的評論,這是一個問題,沒有泡沫設置爲「真實」的孩子,並有一個多餘的內容。在父級的addEventListener中。 – Neilisin 2012-08-10 16:58:39

回答

0

從孩子的MovieClip則需要重新引用到父MC。相信這可以通過類似的東西來完成:

MovieClip(root).dispatchEvent(new Event("directions")); 
+0

謝謝,但I'ms仍然得到父的輸出:'類型錯誤:錯誤#1009:無法訪問空對象的屬性或方法reference.' – Neilisin 2012-08-10 13:00:46

+0

這是相當簡單;在子SWF爲了增加TRUE;到'dispatchEvent'氣泡工作: 'centre_btn.addEventListener(的MouseEvent.MOUSE_DOWN,childBtn_mouseDownHandler);功能childBtn_mouseDownHandler(事件:MouseEvent)方法{dispatchEvent(新的事件(「方向」,真));}' 此外,我想指出的是,你需要從父事件監聽器刪除'.content.': 'if(loader.parent!= null){loader.dispatchEvent(new Event(Event.ADDED_TO_STAGE));跟蹤(「加載到階段的加載器」); loader.addEventListener(「directions」,childLoader_someSignalHandler); }' – Neilisin 2012-08-10 16:57:16