10 Replies Latest reply on Feb 1, 2010 3:17 PM by kglad

    unloading swf

    jreich1 Level 1

      I am new to flash and am trying to create a project with multiple pages.  I stumbled across a tutorial demonstrating how to load and unload swf's.  I used it and it works.  But i was wondering if anyone could help guide me to a way i could specify a btn or mc in the loaded swf that will specify the "click" area where the unload content takes place?  Right now if i click anywhere on the loaded swf it unloads (i want that to be specified).

       

       

      var myLoader:Loader=new Loader  ();

      page1_mc.addEventListener(MouseEvent.CLICK, page1content);

      function page1content(myevent:MouseEvent):void {

      var myURL:URLRequest=new URLRequest("mp3player_ornery.swf");

      myLoader.load(myURL);

      addChild(myLoader);

      page1_mc.gotoAndPlay(1);

      }

      myLoader.addEventListener(MouseEvent.CLICK, unloadcontent);

      function unloadcontent(myevent:MouseEvent):void {

      removeChild(myLoader);

      page1_mc.gotoAndPlay(1);

       

      }

        • 1. Re: unloading swf
          kglad Adobe Community Professional & MVP

          after loading is completed, myLoader.content will reference the main timeline of your loaded swf.  so, if you have a button (say btn), on the main timeline of your loaded swf, after loading completes, you could use:

           

          MovieClip(myLoader.content).btn.addEventListener(MouseEvent.CLICK, unloadcontent);

          function unloadcontent(myevent:MouseEvent):void {

          myLoader.unloadAndStop();  // if you're publishing for fp10, use this line

          removeChild(myLoader);

          page1_mc.gotoAndPlay(1);

           

          }

          • 2. Re: unloading swf
            jreich1 Level 1

            What am I missing here.

            MovieClip(myLoader.content)invisiblebutton_btn.addEventListener(MouseEvent.CLICK, unloadcontent);

            function unloadcontent(myevent:MouseEvent):void {

            myLoader.unloadAndStop();

            removeChild(myLoader);

            page1_mc.gotoAndPlay(1);

             

            }

            Error:

            1087: Syntax error: extra characters found after end of program.

            • 3. Re: unloading swf
              kglad Adobe Community Professional & MVP

              :

               


              MovieClip(myLoader.content).invisiblebutton_btn.addEventListener(MouseEvent.CLICK , unloadcontent);

              function unloadcontent(myevent:MouseEvent):void {

              myLoader.unloadAndStop();

              removeChild(myLoader);

              page1_mc.gotoAndPlay(1);

               

              }


              • 4. Re: unloading swf
                jreich1 Level 1

                Thank You for you help.  Now I'm getting this error. Any suggestions.

                 

                Error:

                1120: Access of undefined property myLoader.

                 

                MovieClip(myLoader.content).invisiblebutton_btn.addEventListener(MouseEvent.CLICK, unloadcontent);

                function unloadcontent(myevent:MouseEvent):void {

                myLoader.unloadAndStop();

                removeChild(myLoader);

                page1_mc.gotoAndPlay(1);

                }

                • 5. Re: unloading swf
                  jreich1 Level 1

                  This is interesting.  I changed some things around:

                   

                  var myLoader:Loader=new Loader  ();

                  MovieClip(myLoader.content).invisiblebutton_btn.addEventListener(MouseEvent.CLICK, unloadcontent);

                  function unloadcontent(myevent:MouseEvent):void {

                  myLoader.unloadAndStop();

                  removeChild(myLoader);

                  }

                   

                  I got this any ideas.

                  TypeError: Error #1009: Cannot access a property or method of a null object reference.

                      at mp3player_ornery_fla::MainTimeline/frame1()

                  • 6. Re: unloading swf
                    kglad Adobe Community Professional & MVP

                    the code you originally showed in your first message with code would not cause an 1120 error.  you must have declared your loader in a function body.  (your last message has code that will cause many problems, so go back to your original code.)

                     

                    and show your original code using the code i suggested.

                    • 7. Re: unloading swf
                      jreich1 Level 1

                      Error: TypeError: Error #1009: Cannot access a property or method of a null object reference.

                          at home_2_fla::categories_mc_5/frame171()

                       

                       

                      var myLoader:Loader=new Loader  ();

                      page1_mc.addEventListener(MouseEvent.CLICK, page1content);

                      function page1content(myevent:MouseEvent):void {

                          var myURL:URLRequest=new URLRequest("mp3player_ornery.swf");

                          myLoader.load(myURL);

                          addChild(myLoader);

                          page1_mc.gotoAndPlay(1);

                      }

                       

                      MovieClip(myLoader.content).invisiblebutton_btn.addEventListener(MouseEvent.CLICK, unloadcontent);

                      function unloadcontent(myevent:MouseEvent):void {

                          myLoader.unloadAndStop();

                          removeChild(myLoader);

                          page1_mc.gotoAndPlay(1);

                      }

                      • 8. Re: unloading swf
                        kglad Adobe Community Professional & MVP

                        that won't work.  as i mentioned, you must wait until loading is complete to reference anything in the loaded swf.  ie, use a complete-event listener.

                        • 9. Re: unloading swf
                          jreich1 Level 1

                          I'm so sorry about my confusion.  Thank you so much for your help.  If you don't mind would you take a look at this and just make sure there is nothing in there that jumps out at you.

                           

                          import flash.events.Event;

                           

                          var myLoader:Loader=new Loader  ();

                          page1_mc.addEventListener(MouseEvent.CLICK, page1content);

                          function page1content(myevent:MouseEvent):void {

                              var myURL:URLRequest=new URLRequest("mp3player_ornery.swf");

                              myLoader.load(myURL);

                              addChild(myLoader);

                              page1_mc.gotoAndPlay(1);

                          }

                          myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);

                          function startListener (e:Event):void{

                          MovieClip(myLoader.content).invisiblebutton2_btn.addEventListener(MouseEvent.CLICK, unloadcontent);

                          }

                          function unloadcontent(myevent:MouseEvent):void {

                          myLoader.unloadAndStop();

                          removeChild(myLoader);

                          page1_mc.gotoAndPlay(1);

                          }

                          • 10. Re: unloading swf
                            kglad Adobe Community Professional & MVP

                            that looks good.  if you're never going to use your loader again, null it.  if you're going to use your loader again, don't forget to add it back to the display list (or don't remove it).