Skip to main content
Known Participant
September 24, 2018
Answered

Unload SWF and Return to a frame in the Initial SWF

  • September 24, 2018
  • 1 reply
  • 1459 views

Hello, I used to use Flash quite often years ago when this was easily done - I guess back in the AS2 days. I have found this difficult to do in AS3. I have created the same presentation in 4 languages. I first created the original English version then duplicated the file to make the conversions. The presentation starts with an initial file titled "Engine360" - Buttons animate in and the time line stops. Viewer then selects which language they prefer by clicking the appropriate button. Let's say the viewer selects "English" The English button directs the play head to a label where the buttons animate off stage, the play head stops on a frame that loads "360English.swf". The code on this frame I used is:

var EnglishLoader:Loader = new Loader();

var url:URLRequest = new URLRequest("360English.swf");

EnglishLoader.load(url);

addChild(EnglishLoader);

This works perfectly, the play head in the initial file "Engine360" is stopped on a blank frame while the viewer watches the loaded "360English.swf". My problem is the viewer needs to be able to select a "Reset" button. The "Reset" button needs to unload the "360English.swf" and return the play head back to the initial "Engine360" say "frame 5" where they can once again select the preferred language. This was once easy, now it's not, but I have spent very little time learning AS3. Any help with this would be immensely appreciated!! I thank you in advance.

Oh, not sure if this is important but this will be published AIR 26.0 for Desktop with Windows installer.

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

    Use the unloadAndStop function from the Loader class.

    Here is a sample where there are two frames in the main timeline. The first one contains two buttons (englishButton and portugueseButton) to change the language.

    In the second frame, the SWFs are actually loaded and there is a reset button.

    AS3 code:

    Frame 1

    import flash.events.MouseEvent;

    import flash.display.Loader;

    import flash.net.URLRequest;

    var swf:String;

    var loader:Loader;

    function changeLanguage(e:MouseEvent):void

    {

         swf = e.currentTarget.name.replace("Button", "") + ".swf";

         gotoAndStop(2);

    }

    function reset(e:MouseEvent):void

    {

         loader.unloadAndStop(true);

         removeChild(loader);

         loader = null;

         gotoAndStop(1);

    }

    stop();

    englishButton.addEventListener(MouseEvent.CLICK, changeLanguage);

    portugueseButton.addEventListener(MouseEvent.CLICK, changeLanguage);

    Frame 2

    loader = new Loader();

    loader.load(new URLRequest(swf));

    addChild(loader);

    resetButton.addEventListener(MouseEvent.CLICK, reset);

    FLA download:

    animate_cc_as3_unload_swf.zip - Google Drive

    I hope this helps.

    Regards,

    JC

    1 reply

    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    September 24, 2018

    Hi.

    Use the unloadAndStop function from the Loader class.

    Here is a sample where there are two frames in the main timeline. The first one contains two buttons (englishButton and portugueseButton) to change the language.

    In the second frame, the SWFs are actually loaded and there is a reset button.

    AS3 code:

    Frame 1

    import flash.events.MouseEvent;

    import flash.display.Loader;

    import flash.net.URLRequest;

    var swf:String;

    var loader:Loader;

    function changeLanguage(e:MouseEvent):void

    {

         swf = e.currentTarget.name.replace("Button", "") + ".swf";

         gotoAndStop(2);

    }

    function reset(e:MouseEvent):void

    {

         loader.unloadAndStop(true);

         removeChild(loader);

         loader = null;

         gotoAndStop(1);

    }

    stop();

    englishButton.addEventListener(MouseEvent.CLICK, changeLanguage);

    portugueseButton.addEventListener(MouseEvent.CLICK, changeLanguage);

    Frame 2

    loader = new Loader();

    loader.load(new URLRequest(swf));

    addChild(loader);

    resetButton.addEventListener(MouseEvent.CLICK, reset);

    FLA download:

    animate_cc_as3_unload_swf.zip - Google Drive

    I hope this helps.

    Regards,

    JC

    DevotedAuthor
    Known Participant
    September 24, 2018

    Thank for the response and examples, however I need the reset button to be contained in the loaded swf, not the initial swf. The reason is I created moving stage animations in Cinema 4D, exported them as a series of .ai files, then imported them into animate as vectors. Basically when the viewer clicks a button in the loaded "360English.swf" the camera moves as if it is inside much larger stage. The camera moves from main stage to a stage above, stage left, right, etc... depending on the viewers selection. If the reset button is viewable in the initial swf it would be very strange as the camera moves and the reset button doesn't. I attached a video of the camera movement inside the loaded "360English.swf" The reset button will be available at the beginning only.

    Basically I need the loaded "360English.swf" to unload itself by clicking reset and return the initial play head to frame 5 in "Engine360"

    The video is the swf I need to unload. The initial swf is not present in this video.

    Thank you for your help, it is greatly appreciated.

    JoãoCésar17023019
    Community Expert
    September 24, 2018

    No problem.

    Just move the reset buttons to each external SWF and paste this code in each of them (in the main timeline):

    import flash.display.MovieClip;

    resetButton.addEventListener(MouseEvent.CLICK, (parent.parent as MovieClip).reset);

    The reset function declaration must stay in the main SWF.

    Regards,

    JC

    P.S.: amazing presentation.