Skip to main content
Known Participant
April 1, 2017
Answered

Unloading an swf movie from the swf itself in AS3

  • April 1, 2017
  • 5 replies
  • 2621 views

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.

This topic has been closed for replies.
Correct answer Colin Holgate

Hello guys, I couldn't find how to attach it directly so here's the dropbox link: Dropbox - closeLoadedSwf.zip

Thanks a lot guys.


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

}

5 replies

sergiopolAuthor
Known Participant
April 2, 2017

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.

sergiopolAuthor
Known Participant
April 1, 2017

Of course I can work a light version so you have at least the elements involved. Thanks Colin.

sergiopolAuthor
Known Participant
April 1, 2017

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.

Colin Holgate
Inspiring
April 1, 2017

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?

sergiopolAuthor
Known Participant
April 1, 2017

Hello guys, I couldn't find how to attach it directly so here's the dropbox link: Dropbox - closeLoadedSwf.zip

Thanks a lot guys.

Colin Holgate
Inspiring
April 1, 2017

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

}

Ned Murphy
Legend
April 1, 2017

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

}

sergiopolAuthor
Known Participant
April 1, 2017

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.