Copy link to clipboard
Copied
I've found many posts in this forum about this but neither solution has worked for me. My setup is as basic as you would think. Main movie loads external swf movie after a mouse click. I need to close that loaded swf from a button located inside the external swf itself. I've come up with codes that won't give errors but won't work either. One thing different in my case is that my main movie is a projector .exe file. I never thought it would be relevant but given the situation I'm starting to think it could be.
I'm loading the external swf with this code:
z5990_btn.addEventListener(MouseEvent.CLICK,loadCard);
function loadCard(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
loadit.load(new URLRequest("casas/Zurich5990.swf"), _lc);
}
Then trying to unload from a code attached to the close button inside external swf...
close_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
MovieClip(this.parent.parent).loadit.unloadAndStop;
}
This is at least the third method I've tried so if anyone could help me I would thank you a lot.
1 Correct answer
There were a few things wrong. You were setting up functions inside other functions, which may actually work but looks odd. In the external swf you had two variations of what we've suggested. Also, dispatchEvent() needs a target, and using the stage is the simplest option. The main problem though was that you weren't listening for the loader being loaded, and so the event listener never gets set up.
Here's a working script from the parent:
stop();
import flash.events.Event;
var loadit: Loader;
z5990_
...Copy link to clipboard
Copied
Children should not tell their parents what to do, they should just cry and let the parents deal with it...
AS3 - Dispatch Event
--------------------
See: http://forums.adobe.com/thread/470135?tstart=120
Example:
Add something to trigger the event in the child:
dispatchEvent(new Event("eventTriggered")); (if dispatchEvent problem, see: http://www.kirupa.com/forum/showthread.php?p=1899603#post1899603)
In your loading/parent swf, listen for the complete event on the Loader.contentLoaderInfo. In the complete event handler, add a listener for the event on the loaded swf.
// event handler triggered when external swf is loaded
function loaderCompleteHandler(event:Event) {
MovieClip(event.currentTarget.content).addEventListener("eventTriggered", eventHandler);
}
function eventHandler(event:Event):void {
trace("event dispatched in loaded swf");
// do your unloading here
}
Copy link to clipboard
Copied
Hahah I see you and Muzak share same sense of humor. That's nice. I guess this covers all my needs, flying trough your response I still have a little doubt on the "do your unloading here" part. I guess here is where I go "unloadAndStop" right? I'll try this and keep you posted on my results. Thank you Ned.
Copy link to clipboard
Copied
Hi again Ned. I tried your suggestions but couldn't make it work. Comparing my code with the one shown here: http://www.kirupa.com/forum/showthread.php?p=1899603#post1899603) I'm missing all the "import flash..." stuff. But when I put it in it gives me lots of errors. I don't know if I need all that to unload a swf. Do I?
Copy link to clipboard
Copied
The examples on that page get more complex than is needed. You are likely to need this line:
import flash.events.Event;
Copy link to clipboard
Copied
Aside from using dispatchevent, which I was going to suggest, make sure that the swf has closed all of its own listeners, including the mouseevent for the exit button. Like:
exitbtn.addEventListener(MouseEvent.CLICK,exit);
function exit(e:MouseEvent){
//remove any other listeners
exitbtn.removeEventListener(MouseEvent.CLICK,exit);
stage.dispatchEvent(new Event("eventTriggered"));
}
Copy link to clipboard
Copied
Oh I forgot to reply to you before Colin. I also added your piece of code. It was pretended for the close button in the loaded (child) movie, right? I added "import flash.events.Event;" and got no errors but still the swf won't unload. Maybe I'm not writting my code correctly since in that frame I have lots of actions that aren't related to the load/unload action. I guessed that was how it was supposed to be. Some time ago you attached most actions to buttons itself so the action layer now gets a little crowded/messy. I know it's a lot more convenient having all the code in one place though. Should I share my .fla? Thanks for the help Colin.
Copy link to clipboard
Copied
Would you be willing to post a zip file that contains enough of your code for us to try to see the problem for ourselves?
Copy link to clipboard
Copied
Hello guys, I couldn't find how to attach it directly so here's the dropbox link: Dropbox - closeLoadedSwf.zip
Thanks a lot guys.
Copy link to clipboard
Copied
There were a few things wrong. You were setting up functions inside other functions, which may actually work but looks odd. In the external swf you had two variations of what we've suggested. Also, dispatchEvent() needs a target, and using the stage is the simplest option. The main problem though was that you weren't listening for the loader being loaded, and so the event listener never gets set up.
Here's a working script from the parent:
stop();
import flash.events.Event;
var loadit: Loader;
z5990_btn.addEventListener(MouseEvent.CLICK, loadCard);
function loadCard(event: MouseEvent) {
loadit = new Loader();
addChild(loadit);
var _lc: LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
loadit.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loadit.load(new URLRequest("Zurich5990.swf"), _lc);
}
function loaderCompleteHandler(event: Event) {
loadit.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
stage.addEventListener("eventTriggered", categoryClickHandler);
}
function categoryClickHandler(event: Event): void {
loadit.unloadAndStop();
}
and here's the working one from the external swf:
stop();
close_btn.addEventListener(MouseEvent.CLICK, exit);
function exit(e: MouseEvent) {
close_btn.removeEventListener(MouseEvent.CLICK, exit);
stage.dispatchEvent(new Event("eventTriggered"));
}
Copy link to clipboard
Copied
Hello Colin, I tried the code and worked perfectly. Thanks again for giving it to me and for your approach to the subject. Not only you help me resolve the issue but you taught me what you were doing in the process. That's awesome. I extend my gratitude to Ned Murphy​ you both have been grate and I hope I can count on your help in the future as I'm sure I'll be needing it. Keep up the good work.
Copy link to clipboard
Copied
Thanks. We're here if something else comes up.
Copy link to clipboard
Copied
This Forum conversation was a Godsend to me. But there is still something that I am missing.
Except for substituting my own instance names and file names, I used the exact code in the reply above. The swf from that code loads the swf file perfectly. But the AIR app will not load that swf.
Is there something else I should know.
Thanks!!
Copy link to clipboard
Copied
AIR for mobile, at least iOS, can't load SWFs that have code in them. I'm not sure about AIR for desktop.
There are complex workarounds. Here's a blog post about the technique:
blogs.adobe.com/airodynamics/2013/03/08/external-hosting-of-secondary-swfs-for-air-apps-on-ios/
Short version is that you can only load swfs that were declared when the app was published, you can't load a swf with code in it that the app doesn't already have the code embedded.
Copy link to clipboard
Copied
I am only working on AIR for desktop. Sorry for not including that detail.
Copy link to clipboard
Copied
Thanks, Colin!
How can I learn about loading local SWFs into AIR app for desktop?
Copy link to clipboard
Copied
Of course I can work a light version so you have at least the elements involved. Thanks Colin.
Copy link to clipboard
Copied
That is kind of embarrassing Colin but I knew it would happen. I forgot to warn you though . Thank you very much for providing me a clean code. I'll use it and give you the corresponding feedback.

