Adding an event listener to a loader in Actionscript 3

I'm building an application in Flash CS5 which loads an external SWF and displays it on the stage. Once the external SWF is loaded and displayed on the stage, the user clicks through a series of buttons in the loaded SWF. The final button click removes the SWF from the stage and another object (this time from the library) is then displayed and the program moves on.

I've tried adding an event listener to the loader so that it can respond to the event REMOVED_FROM_STAGE. However when the external SWF is removed from the stage, nothing happens.

What you need to do is to dispatch an event from the external swf.
Assuming that your external swf is named "myExternalClip.swf" and assuming that it contains a button named "ImDone" (the last button that you mentioned) you can do the following:

Go to the myExternalClip.fla, go to the actionscript editor and add normally an event listener for the button that you want to signal the end of this clip, in this case the ImDone button.

ImDone.addEventListener(MouseEvent.CLICK, superIamDone);

then add the superIamDone function (ofcourse you can name the function whatever you want:) :

function superIamDone(event):void
{
//enter here whatever code you need to do what the button does PLUS the code below.
dispatchEvent(new Event("Done"));
}

this last line will dispatch an event similar to mouse or other events that can be "heard" by anyone listening for such an event.

Now go to the parent fla, the one that loads the external URL and add code that 1st will load the external clip and then it will do something when it will "hear" the Done message from this external clip.

Thanks for getting back to me. I've altered the code as you suggest. The external swf loads and performs OK, but when I hit the finish button the external swf is not removed from the stage. The parent application doesn't seem to be hearing the event from the external swf.

1. "External.swf" loads OK and is placed on the stage.
2. "External.swf' does everything it's supposed to do.
3. When the finish button on "External.swf" is clicked, "The finish button has been clicked" is output, so no problem there.

However, the program never moves into the function doneHandler, as no trace comes up "I have made it to here."

if i may jump in. your code does work
check that you're not getting any compile errors.
I've just replicated your code and the trace ("i have made it to here") pops up
i've attached my sample.dispatcher.zip

if i may jump in. your code does work
check that you're not getting any compile errors.
I've just replicated your code and the trace ("i have made it to here") pops up
i've attached my sample.dispatcher.zip

Thank you for jumping in! And for demonstrating that the code does in fact work. I was starting to worry about my sanity.

I've downloaded your file and run it on my computer, and it works. I then removed your buttons and substituted them for a couple of Illustrator buttons that I'm using, and it still works.

But when I run my original program, which consists of a movieclip made up of Illustrator vector files, I cannot get "I have made it to here". I'll deconstruct everything and build it back up to see if I can find an error.

Are you aware of Illustrator vector files causing unexpected errors in program operation?

I've done some work on this problem and I've come to the following conclusions.

1. I can make up a main program and externalSWF program using simple buttons made in Flash and the code works perfectly.
2. I can then substitute the simple Flash buttons for custom made Illustrator buttons and the code works perfectly.
3. I then put the complete movieclip into the externalSWF file (using the custom Illustrator buttons that previously worked) and the code no longer works.

i'm not sure i understand what you mean by "put the complete movieclip into the externalSWF" if you're doing what i think you're doing, it might be a pathing issue, but upload it so we can have a look.

Thanks to both of you for your help. I still haven't been able to get my external swf to dispatch an event back to the main program, but I've picked up some very useful knowledge along the way. I have notice some of my movieclips that rely on imported Illustrator vector files sometimes do odd things.

0

Featured Post

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…

I have found that much of my time doing support ends up being a constant repetition of the same steps to different people. Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden. Sometimes this jus…

The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast.
Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…