Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Unloading an swf movie from the swf itself in AS3

Explorer ,
Mar 31, 2017 Mar 31, 2017

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.

TOPICS
ActionScript
2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 01, 2017 Apr 01, 2017

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_

...
Translate
LEGEND ,
Apr 01, 2017 Apr 01, 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2017 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2017 Apr 01, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2017 Apr 01, 2017

The examples on that page get more complex than is needed. You are likely to need this line:

import flash.events.Event;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2017 Apr 01, 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"));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2017 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2017 Apr 01, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2017 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2017 Apr 01, 2017

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2017 Apr 02, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 02, 2017 Apr 02, 2017

Thanks. We're here if something else comes up.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 31, 2018 Jan 31, 2018

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!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2018 Jan 31, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2018 Feb 01, 2018

I am only working on AIR for desktop. Sorry for not including that detail.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2018 Feb 01, 2018
LATEST

Thanks, Colin!

How can I learn about loading local SWFs into AIR app for desktop?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 01, 2017 Apr 01, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2017 Apr 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines