12 Replies Latest reply: Mar 14, 2012 4:13 PM by kglad RSS

    How to effectively remove a loaded SWF from the stage?

    nikolaig Community Member

      I can not figure out a proper coding to remove a loded SWF from the stage.

      Here is my set up.

      I have a layout segmented into labeled section. In the section labeled "products" I have a layout consisting of product images acting as buttons which bring a user to another labeled section "prdctsPopUps"

      In the "prdctsPopUps" section I have placed an instance of LoaderMax placed into an mc container. Placing LoaderMax into an mc container automatically resolved an issue of clearing loaded SWFs from stage when I come back to "products" section.

      I specified the variable in the "products" section with the following set up:

       

      var sourceVar_ProductsPopUps:String;

       

      function onClickSumix1PopUp(event:MouseEvent):void {

                          sourceVar_ProductsPopUps="prdcts_popups/sumix1-popup_tl.swf";

                          gotoAndPlay("prdctsPopUps");

                          }

       

      So each button has its own "....swf" URL and they all open fine and I can come back to "products" section without any issues.

       

      However inside the swf (which loads through LoaderMax which is placed into an mc) there are other buttons which bring a user to labeled section "xyz". Which also functions properly. It opens as it is supposed to be and without any previously loaded "...swf" on the stage.

      At the labeled section "xyz" there is a limited set of buttons repeating from section "products" which has to bring a user back to the same set up in the "prdctsPopUps" labeled section and open a corresponding "...swf" .

      However only the last opened "...swf" will appear in that section. Effectively the one which was originally opened from the "prdctsPopUps" section and not the one which was supposed to be opened from the "xyz" section.

       

      I can not understand why it would work from one labeled section and not from another. I can not figure out on which section which code/function needed to be placed.

       

      Here is the set up from a button from the "xyz" section whcih supposed to bring a user to the same "prdctsPopUps" section but to load a different "...swf"

       

      var sourceVar_ProductsPopUps_fromXYZ:String;

       

      function onClick_floralytePopUp_fromXYZ(event:MouseEvent) :void {

                          sourceVar_ProductsPopUps_fromXYZ="prdcts_popups/floralyte-popup_tl.swf";

                          gotoAndPlay("prdctsPopUps");

       

                          }

       

       

      Here is the code set up for the LoaderMax from the "prdctsPopUps" section:

       

      var loaderProductPopUps:SWFLoader = new SWFLoader(sourceVar_ProductsPopUps,

                                                                                                          {

                                                                                                          estimatedBytes:5000,

                                                                                                          container:holderMovieClip,

                                                                                                          onProgress:progressHandler,

                                                                                                          onComplete:completeHandler,

                                                                                                          centerRegistration:true,

                                                                                                          alpha:1,

                                                                                                          scaleMode:"none",

                                                                                                          width:540,

                                                                                                          height:730,

                                                                                                          crop:true,

                                                                                                          autoPlay:false

                                                                                                          });

       

       

      function progressHandler(event:LoaderEvent):void{

                progressBarPopUp_mc.gradientbarPopUp_mc.scaleX = loaderProductPopUps.progress;

      }

       

       

      function completeHandler(event:LoaderEvent):void{

                var loadedImage:ContentDisplay = event.target.content;

                TweenMax.to(progressBarPopUp_mc, 1.5, {alpha:0, scaleX:0.25, scaleY:0.25});

                }

       

       

      loaderProductPopUps.load();

       

      Is there something which needs to be imported, or specific function needs to be specified in a specific labeled section?

        • 1. Re: How to effectively remove a loaded SWF from the stage?
          kglad CommunityMVP

          if you're publishing for fp10+, you should use unloadAndStop() applied to your loader, to unload a loaded swf.  otherwise, use unload() after explictly stopping all streams (video, music) in the loaded swf.

          • 2. Re: How to effectively remove a loaded SWF from the stage?
            nikolaig Community Member

            I am publishing for the fp10+.

            My loader named "loaderProductPopUps" is placed into an mc container named "holderMov ieClip". Should I use the name of the loader? Then my function will look like this:

              loaderProductPopUps.unloadAndStop();

             

            My complication is that I can not figure out where the code placement has to be?

             

            A. In the frame with the loader on the main time line. If yes then before or after specifiying that it loads with the command "loaderProductPopUps.load();"

             

            B. In the loaded SWF file?. Then I can apply it to the close button which closes the loader and sends it to another labeled section from where the loader is accessed again.

            Here is the code sample for the close button from inside the SWF.

            Note that in order to access the time line of the main file I had to use the following set up:

              MovieClip(parent.parent.parent).gotoAndPlay("products");

            does it mean that for the unloadAndStop()  I will have to use similar set up like this?

            MovieClip(parent.parent.parent).unloadAndStop();

             

            //////////////////////////////////closeX_btn

            closeX_btn.closeX_ON.alpha = 0;

                                var  closeX_Tween:TweenLite = TweenLite.to(closeX_btn.closeX_ON, .25, {alpha:1, paused:true});

                    

                                closeX_btn.addEventListener(MouseEvent.ROLL_OVER, overHandler_closeX_btn);

                                closeX_btn.addEventListener(MouseEvent.ROLL_OUT, outHandler_closeX_btn);

             

                    closeX_btn.addEventListener(MouseEvent.CLICK, onClick_closeX_btn);

             

             

            function onClick_closeX_btn(event:MouseEvent) :void {

                      MovieClip(parent.parent.parent).gotoAndPlay("products");

                                }

             

             

                      function overHandler_closeX_btn(e:MouseEvent):void{

                     closeX_Tween.play();

                     trace("you rolled over me");

                     }

                    

                      function outHandler_closeX_btn(e:MouseEvent):void{

                     closeX_Tween.reverse();

                     trace("you rolled off me");

                     }

             

             

            //////////////////////////////////

             

            C. In the frame from where the loader is accessed? It means I would visit the frame with the loader, navigate out, and then navigate back.

            So that frame to which I navigate to second time around and then navigate from to the loader to access it for the second time. Each button in this fram would have to have this unloadAndStop() function?

            • 3. Re: How to effectively remove a loaded SWF from the stage?
              sinious CommunityMVP

              SWFLoader is some class (we can't see) that contains the Loader. If it implements a way to unload what it is currently trying to load or not is up to you to find out. It probably has a way to do it but we can't see the code for the class so you're going to have to check documentation.

               

              unloadAndStop() should be run directly on a Loader. It is a method of the Loader class. Since you only have a reference to whatever 'SWFLoader' is, we can't tell you how to gain access to the Loader inside it.

               

              Hopefully whatever SWFLoader is will be smart enough to re-use the same loader and properly dispose of the previously loaded content. If you instantiate a new SWFLoader for each separate thing you load then there should be a dispose method of some sort you use to have that class remove the SWF properly. Then dispose of the reference to SWFLoader (get it off the display list if it's on, null the reference) when you're done with it.

              • 4. Re: How to effectively remove a loaded SWF from the stage?
                nikolaig Community Member

                Well, I am not usin and SWF loader I am using LoaderMax from greensock.com

                But I thought it is more of the AS3 problem rather than LoaderMax problem

                 

                How would I apply the dispose method?

                Could you give me a sample code and where it has to be implemented.

                On the same frame with the loader or in the swf which closes the loader?

                • 5. Re: How to effectively remove a loaded SWF from the stage?
                  kglad CommunityMVP

                  use:

                   

                  loaderProductPopUps.unloadAndStop()

                  • 6. Re: How to effectively remove a loaded SWF from the stage?
                    nikolaig Community Member

                    O.K. This is how I placed it :

                    ----------------------------------------------------------------------------------------

                     

                    var loaderProductPopUps:SWFLoader = new SWFLoader(sourceVar_ProductsPopUps,

                                                                                                                         {

                                                                                                                         estimatedBytes:5000,

                                                                                                                         container:holderMovieClip,

                                                                                                                         onProgress:progressHandler,

                                                                                                                         onComplete:completeHandler,

                                                                                                                         centerRegistration:true,

                                                                                                                         alpha:1,

                                                                                                                         scaleMode:"none",

                                                                                                                         width:540,

                                                                                                                         height:730,

                                                                                                                         crop:true,

                                                                                                                         autoPlay:false

                                                                                                                         });

                     

                     

                    function progressHandler(event:LoaderEvent):void{

                              progressBarPopUp_mc.gradientbarPopUp_mc.scaleX = loaderProductPopUps.progress;

                    }

                     

                     

                    function completeHandler(event:LoaderEvent):void{

                              var loadedImage:ContentDisplay = event.target.content;

                              TweenMax.to(progressBarPopUp_mc, 1.5, {alpha:0, scaleX:0.25, scaleY:0.25});

                              }

                     

                     

                    loaderProductPopUps.unloadAndStop();

                     

                     

                    loaderProductPopUps.load();

                     

                    ----------------------------------------------------------------------------------------

                     

                    and got the following error message:

                    1061: Call to a possibly undefined method unloadAndStop through a reference with static type com.greensock.loading:SWFLoader.

                    • 7. Re: How to effectively remove a loaded SWF from the stage?
                      kglad CommunityMVP

                      for gs' swfloader, use unload().

                       

                      and you need to apply that when you want to unload your swf:

                       

                       

                      if(loaderProductPopUps.content){

                      loaderProductPopUps.unload();

                      }

                      • 8. Re: How to effectively remove a loaded SWF from the stage?
                        nikolaig Community Member

                        Here is where I placed the code. Right above the loaderProductPopUps.load();

                        It does not give me any more errors, but still does not unload the previously loaded swf.

                        Here is the entire line chunk of code:

                         

                        var loaderProductPopUps:SWFLoader = new SWFLoader(sourceVar_ProductsPopUps, //the value of sourceVar_ProductsPopUps allows to load mulitple SWFs from the products page.

                                                                                                                            {

                                                                                                                            estimatedBytes:5000,

                                                                                                                            container:holderMovieClip,// more convinient and easier to manage if to place the LoaderMax into an empty mc (holderMovieClip)

                                                                                                                                                                                          // if not will work as well. Then the line container:holderMovieClip, has to be replaced with container:this,

                                                                                                                                                                                          // can be any size, can not be scaled as it distorts the content

                                                                                                                            onProgress:progressHandler,

                                                                                                                            onComplete:completeHandler,

                                                                                                                            centerRegistration:true,

                                                                                                                            //x:-260, y:-320, //no need for this is if used: centerRegistration:true,

                                                                                                                            alpha:1,

                                                                                                                            scaleMode:"none",

                                                                                                                            //scaleX:0, scaleY:0,

                                                                                                                            //vAlign:"top",

                                                                                                                            width:540,

                                                                                                                            height:730,//scales proportionally but I need to cut off the edges

                                                                                                                            crop:true,

                                                                                                                            autoPlay:false

                                                                                                                            });

                         

                         

                        function progressHandler(event:LoaderEvent):void{

                                  progressBarPopUp_mc.gradientbarPopUp_mc.scaleX = loaderProductPopUps.progress;

                        }

                         

                         

                        function completeHandler(event:LoaderEvent):void{

                                  var loadedImage:ContentDisplay = event.target.content;

                                  //TweenMax.to(loadedImage, 1.5, {alpha:1, scaleX:1, scaleY:1});//only need this line if corresponding values are changed in SWF loader constructor

                                  TweenMax.to(progressBarPopUp_mc, 1.5, {alpha:0, scaleX:0.25, scaleY:0.25});

                                  }

                         

                         

                        if(loaderProductPopUps.content){

                        loaderProductPopUps.unload();

                        }

                         

                         

                        loaderProductPopUps.load();

                        • 9. Re: How to effectively remove a loaded SWF from the stage?
                          sinious CommunityMVP

                          Is this on a frame script? If it is and it's running every time you get in a frame then you're creating a new instance of loaderProductPopUps every time, so of course it won't have any .content.

                           

                          This should be at the TOP of all the code, not the bottom, before you create a new instance of loaderProductPopUps:

                           

                          if ((loaderProductPopUps != null)&&(loaderProductPopUps.content))

                          {

                             loaderProductPopUps.unload();

                          }

                          • 10. Re: How to effectively remove a loaded SWF from the stage?
                            kglad CommunityMVP

                            actually, i think you'll need to use something like:

                             

                             

                             

                            var loaderProductPopUps:SWFLoader;

                             

                            if ((loaderProductPopUps){

                            if(loaderProductPopUps.content)){

                               loaderProductPopUps.unload();

                            }

                            }

                             

                            loaderProductPopUps= new SWFLoader(sourceVar_ProductsPopUps, //the value of sourceVar_ProductsPopUps allows to load mulitple SWFs from the products page.

                                                                                                                                 {

                                                                                                                                 estimatedBytes:5000 ,

                                                                                                                                 container:holderMov ieClip,// more convinient and easier to manage if to place the LoaderMax into an empty mc (holderMovieClip)

                                                                                                                                                                                                 // if not will work as well. Then the line container:holderMovieClip, has to be replaced with container:this,

                                                                                                                                                                                                 // can be any size, can not be scaled as it distorts the content

                                                                                                                                 onProgress:progress Handler,

                                                                                                                                 onComplete:complete Handler,

                                                                                                                                 centerRegistration: true,

                                                                                                                                 //x:-260, y:-320, //no need for this is if used: centerRegistration:true,

                                                                                                                                 alpha:1,

                                                                                                                                 scaleMode:"none",

                                                                                                                                 //scaleX:0, scaleY:0,

                                                                                                                                 //vAlign:"top",

                                                                                                                                 width:540,

                                                                                                                                 height:730,//scales proportionally but I need to cut off the edges

                                                                                                                                 crop:true,

                                                                                                                                 autoPlay:false

                                                                                                                                 });

                             

                             

                            function progressHandler(event:LoaderEvent):void{

                                      progressBarPopUp_mc.gradientbarPopUp_mc.scaleX = loaderProductPopUps.progress;

                            }

                             

                             

                            function completeHandler(event:LoaderEvent):void{

                                      var loadedImage:ContentDisplay = event.target.content;

                                      //TweenMax.to(loadedImage, 1.5, {alpha:1, scaleX:1, scaleY:1});//only need this line if corresponding values are changed in SWF loader constructor

                                      TweenMax.to(progressBarPopUp_mc, 1.5, {alpha:0, scaleX:0.25, scaleY:0.25});

                                      }

                             

                             

                             

                            loaderProductPopUps.load();

                            • 11. Re: How to effectively remove a loaded SWF from the stage?
                              nikolaig Community Member

                              Thanks, this seems to work.

                              I had to change your code a little bit however.

                              It seems you had a couple of extra parenthesis in lines 3 and 4

                              Now the begining of your code looks like this:

                               

                              var loaderProductPopUps:SWFLoader;

                               

                              if (loaderProductPopUps){

                              if(loaderProductPopUps.content){

                                 loaderProductPopUps.unload();

                              }

                              }

                               

                              Please let me know if for some reason I did not have to remove the ((

                              • 12. Re: How to effectively remove a loaded SWF from the stage?
                                kglad CommunityMVP

                                you're correct.  i see an extra right paranthesis in this line:

                                 

                                if(loaderProductPopUps.content)){