0

I've recently taken on a project which is loading SWFs into the child SWFs which not only is messy but also slow and incomprehensible to debug.

Therefore, I'm trying to find the correct procedure to call back and forth between a parent and child SWF (bubbles). I'm assuming this is the right way to go about this: loading/unloading and moving playhead.

I hear the dispatchEvent() function is used from child to parent? but I'm not sure how this is interpreted. Would I need to use an addEventListener() in the parent to listen out for the dispatchEvent()?

Can you tell me how I would tell the parent SWF to move the playhead to another position on the parent timeline?

Would I use?:

child_btn.AddEventListener(MouseEvent.MOUSE_DOWN, movetolabel)

function movetolabel() {
dispatchEvent(parent.gotoAndStop("label"));
}
Neilisin
  • 27
  • 10

1 Answers1

0

Inside the main class of the loaded SWF:

// The 3rd parameters enables capture of bubbling events
childBtn.addEventListener(MouseEvent.MOUSE_DOWN, childBtn_mouseDownHandler);

function childBtn_mouseDownHandler(event:MouseEvent) {
    dispatchEvent(new Event("someSignal"));
}

Inside the main class of the parent SWF:

childLoader.content.addEventListener("someSignal", childLoader_someSignalHandler);

function childLoader_someSignalHandler(event:Event):void {
    gotoAndStop("label");
}
Florent
  • 12,310
  • 10
  • 49
  • 58
  • Hi thanks for this response! However, even with a trace in the function nothing is returned. How do I call outside of the current SWF to its parent? – Neilisin Jul 30 '12 at 16:06
  • The loaded SWF can't control the parent SWF. The parent SWF has to attach event handlers on the loaded SWF and has to take actions when an event is fired. – Florent Jul 30 '12 at 16:10
  • Hey there, thanks for all your help! By _main class_ does that mean I would require a class in the parent swf? Currently there are no classes. Is this a problem? – Neilisin Jul 30 '12 at 20:03
  • I think you are using Flash (not Flash Builder). When I say _main class_ i mean _document class_. [Further information here](http://www.graphicmania.net/understanding-the-the-document-class-in-flash/). – Florent Jul 31 '12 at 07:34
  • Florent, thank you so much for your help. I was really struggling to find documentation that explains some of the fundamentals of AS. Trawling one forum to another never seemed to explain what i was looking for. I may need to write a class that loads each SWF instead of a function for each. :) I'm sure more questions will come. – Neilisin Jul 31 '12 at 12:14
  • Hi Florent, I've revisited this problem and I'm finding that the `Event.ADDED_TO_STAGE` hasn't been called, I've been plugging it in using an `if (Event.ADDED_TO_STAGE != null) {` but still having difficulty with this. Any ideas? :) – Neilisin Aug 10 '12 at 09:21
  • If `clip.parent` is not `null`, then `clip` was added to stage. In this case, trigger manually the event. `if (clip.parent != null) clip.dispatchEvent(new Event(Event.ADDED_TO_STAGE));` – Florent Aug 10 '12 at 09:31
  • Hi again, I'm getting "Error #1009: Cannot access a property or method of a null object reference." `if (loader.parent != null) { loader.dispatchEvent(new Event(Event.ADDED_TO_STAGE)); trace("loader added to stage"); loader.content.addEventListener("directions", childLoader_someSignalHandler); } function childLoader_someSignalHandler(event:Event):void { gotoAndStop("directions"); }` But I'm not seeing how this is happening. the trace returns fine. – Neilisin Aug 10 '12 at 10:21
  • Maybe you should open a new question. – Florent Aug 10 '12 at 11:32