0

I'm struggling with communicating from a child to parent SWF in a project, where the parent is calling a child from within a movieclip.

The parent movieclip has this in the actions layer

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

The child SWF has this code sending to the parent:

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

The error I'm being returned is:

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

Neilisin
  • 27
  • 10
  • It's not clear, from your example, which line of code is causing this error. When the button is clicked in the child SWF, does the code in `childLoader_someSignalHandler()` get executed? – Sunil D. Aug 10 '12 at 16:06
  • The answer is on the comment below, it was a problem with both not having bubbles set to `true` in the child and there was a redundant .content. in the addEventListener in the parent. – Neilisin Aug 10 '12 at 16:58

1 Answers1

0

From the child MovieClip you would need to reference back up to the parent MC. Believe this can be done through something like:

MovieClip(root).dispatchEvent(new Event("directions"));
Ross McLellan
  • 1,872
  • 1
  • 15
  • 19
  • Thanks but I'ms still getting an output in the parent: `TypeError: Error #1009: Cannot access a property or method of a null object reference.` – Neilisin Aug 10 '12 at 13:00
  • It was rather simple; in the child SWF add `true` to the `dispatchEvent` in order for bubbles to work: `centre_btn.addEventListener(MouseEvent.MOUSE_DOWN, childBtn_mouseDownHandler); function childBtn_mouseDownHandler(event:MouseEvent) { dispatchEvent(new Event("directions", true));}` Also I would like to point out that you need to remove `.content.` from the event listener in the parent: `if (loader.parent != null) { loader.dispatchEvent(new Event(Event.ADDED_TO_STAGE)); trace("loader added to stage"); loader.addEventListener("directions", childLoader_someSignalHandler); }` – Neilisin Aug 10 '12 at 16:57