16 Replies Latest reply: Aug 5, 2011 9:40 PM by ellenst RSS

    Controlling multiple external swfs from main timeline

    ellenst

      Hi, I've been trying to combine the features of two tutorials I found, one that deals with controlling external swf's from the main timeline and the other that explains how to load and unload multiple external swf's.  I'd like to be able to load and unload multiple external swf's, and control these swf's buttons from the main timeline. 

       

      I asked this same question on the forum of the site where I found these tutorials, and receive only one reply saying that "it should'nt be hard to do." but they did not elaborate any further.  This left me think that I was missing something very obvious and I've spent too much time now trying to work what it is now.  I understand pretty much how both pieces of code work, but just can't work out how to combine them.

       

      If someone could explain it to me, I'd much appreciate it.

       

       

      Tutorial One code - controlling an external swf from the main timeline.

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

      var ldr:Loader = new Loader();

      var urlReq:URLRequest = new URLRequest("swfs/balls.swf");

      ldr.load(urlReq);

       

      function loadHandler (event:Event) {

       

      var myClip:MovieClip = event.target.content;

      addChild(myClip);

       

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

      function myClipOver(event:MouseEvent):void {

      myClip.myBlueBalls.stop();

       

      }

      function myClipOut(event:MouseEvent):void {

      myClip.myBlueBalls.play();

       

      }

      // set listeners

      myClip.addEventListener(MouseEvent.ROLL_OVER, myClipOver);

      myClip.addEventListener(MouseEvent.ROLL_OUT, myClipOut);

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

       

      }

       

      // listener

      ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);

       

      Tutorial Two code - loading and unloading multiple external swfs.

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

      var Xpos:Number = 110;

      var Ypos:Number = 180;

      var swf:MovieClip;

      var loader:Loader = new Loader();

       

      var defaultSWF:URLRequest = new URLRequest("swfs/eyesClosed.swf");

       

      loader.load(defaultSWF);

      loader.x = Xpos;

      loader.y = Ypos;

      addChild(loader);

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

       

      // Btns Universal function

      function btnClick(event:MouseEvent):void {

       

      removeChild(loader);

      var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");

      loader.load(newSWFRequest);

      loader.x = Xpos;

          loader.y = Ypos;

      addChild(loader);

      }

      // Btn listeners

      eyesClosed.addEventListener(MouseEvent.CLICK, btnClick);

      stingray.addEventListener(MouseEvent.CLICK, btnClick);

      demon.addEventListener(MouseEvent.CLICK, btnClick);

      strongman.addEventListener(MouseEvent.CLICK, btnClick);

        • 1. Re: Controlling multiple external swfs from main timeline
          Ned Murphy CommunityMVP

          If you stick with the first tutorial you show it has the basics that address what you say you want to do.  The two things I would suggest are

           

          1)  moving the last line...

           

             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);

           

          just before the line where the load command is issued.  Loader listeners should be assigned before loading is started.

           

          2)  moving the two functions that are nested inside the loadHandler function out of there.  You should never nest named functions.

           

          The first lines set up the Loader to load the external file.  The listener will detect when the loading is complete and then call the  loadHandler function.  The loadHandler function takes the target content of the event target, which is the swf that was loaded and assigns it to a movieclip.  THat movieclip then has two listeners assigned to it for mouse interactions.

          • 2. Re: Controlling multiple external swfs from main timeline
            ellenst Community Member

            Hi Ned, thanks for your reply. I have made those adjustments the code as suggested, thanks. 

             

            What I am hoping to do is to incorporate the "Btns Universal function" from the second tutorial into the first tutorial, so, if you click a btn inside the externally loaded swf, it triggers an eventListener on the main timeline to unload that external swf and replace it with a new external swf.  Is this possible? 

            • 3. Re: Controlling multiple external swfs from main timeline
              Ned Murphy CommunityMVP

              Yes, what you should do is just have the loaded swf's button(s) dispatch an event and have the parent swf set up listeners for the event(s) as soon as the loading is complete.

               

              Example:

               

              Add something to trigger the event in the child, in your case the event handler of a button:


                 dispatchEvent(new Event("event1Triggered"));


              In your loading/parent swf, listen for the complete event on the Loader.contentLoaderInfo (which your first example already has).  In the complete event handler (your "loadHandler" function), add a listener for the event on the loaded swf.


                 function loadHandler(event:Event) {
                     (event.currentTarget.content as MovieClip).addEventListener("event1Triggered", event1Handler);
                 }

               

                 function event1Handler(event:Event):void {
                     trace("category button clicked in loaded swf");
                 }

              • 4. Re: Controlling multiple external swfs from main timeline
                relaxatraja Community Member

                I created an example for you to know it clearly what Ned Merphy said:

                 

                http://trulyraja2009.byethost12.com/DispatchingSwf.zip

                 

                Dont click the link, copy and paste it in the address bar and download it.

                • 5. Re: Controlling multiple external swfs from main timeline
                  ellenst Community Member

                  Hi relaxatraja, thanks for the files, but I can't open them.  I'm still using CS4, is that's the reason why?

                  • 6. Re: Controlling multiple external swfs from main timeline
                    ellenst Community Member

                    Thanks Ned, your help is much appreciated.  As you may have noticed, this is my first post here.  I try to work out as much as I can by myself, via tutorials and the posts of others, although, that now seems fraught with its own problems, as you pointed out.  So, I'm not a total newbie, but the more I learn the more I realise I am still a bit of a Sergeant Shultz "I KNOW NOTHING", which is a little daunting.  Anyway, thanks again, I'll see if I can make this work.

                     

                    • 7. Re: Controlling multiple external swfs from main timeline
                      Ned Murphy CommunityMVP

                      You're welcome.  If you have a problem implementing something I described, just ask.  As I mentioned in my last response, you have some of what you need in place and just need to add to it.  For the dispatchEvent line of code you just need to plant that in the event handler function of whatever button you plan to use for it in the swf you are loading.  And the loadHandler function I show is the same one you already have... I just didn't show the rest of the code you have in it.

                      • 8. Re: Controlling multiple external swfs from main timeline
                        relaxatraja Community Member

                        Yes it was in CS5, I updated it. Download it now for a reference.

                        • 9. Re: Controlling multiple external swfs from main timeline
                          ellenst Community Member

                          Hi relaxatraja,  thanks for changing your FLA for me.  I have managed to get your code to load and unload an external SWF, but I can't workout how to get it to load the replacement SWF - triggered by clicking a button inside the external SWF that is being unloaded.  This is the code I have tried inside the external SWF, but it has no effect.  Can you let me know where I am going wrong?

                           

                          stop();

                          import flash.events.MouseEvent;

                           

                          magBrock.addEventListener(MouseEvent.CLICK,goExit);

                          magRacer.addEventListener(MouseEvent.CLICK,goExit);

                           

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

                          function goExit(e:MouseEvent):void {

                               dispatchEvent(new Event("close"));

                          }

                           

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

                          magBrock.addEventListener(MouseEvent.CLICK,replaceSWF);

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

                          function replaceSWF(e:MouseEvent):void {

                               dispatchEvent(new Event("swfs/printBrock01.swf"));

                          }

                           

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

                          magRacer.addEventListener(MouseEvent.CLICK,replaceSWF);

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

                          function replaceSWF(e:MouseEvent):void {

                               dispatchEvent(new Event("swfs/printRacer01.swf"));

                          }

                          • 10. Re: Controlling multiple external swfs from main timeline
                            ellenst Community Member

                            Hi Ned, sorry I haven't replied till now, I've been snowed under with other work, but I have been going back and forth over this for the last few days, and can't see where I'm going wrong.

                             

                            I have tried the code that relaxatraja was nice enough to post and it loads and unloads the external SWF, but I don't understand how to get it to load the replacement SWF. I have asked him to explain that for me.  But I would also like to follow this bit of code through too, so I can learn what I am doing wrong.

                             

                            This is the code I'm trying -

                             

                            stop();

                            var Xpos:Number = 0;

                            var Ypos:Number = 0;

                            var myClip:MovieClip

                             

                            var ldr:Loader = new Loader();

                            var urlReq:URLRequest = new URLRequest("swfs/external01.swf");

                             

                            // listener

                            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);

                             

                            ldr.load(urlReq);

                             

                            function loadHandler (event:Event) {

                             

                            removeChild(myClip);

                            var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");

                            ldr.load(newSWFRequest);

                            ldr.x = Xpos;

                                ldr.y = Ypos;

                            addChild(myClip);

                            }

                            //But, I am getting this error message -
                            TypeError: Error #2007: Parameter child must be non-null.
                            at flash.display::DisplayObjectContainer/removeChild()
                            at main_fla::MainTimeline/loadHandler()
                            //This is the code I have inside the external SWF which is not loading the replacement SWF even using relaxatrja's code

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

                            stop();

                            import flash.events.MouseEvent;

                             

                            magBrock.addEventListener(MouseEvent.CLICK,goExit);

                            magRacer.addEventListener(MouseEvent.CLICK,goExit);

                             

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

                            function goExit(e:MouseEvent):void {

                                 dispatchEvent(new Event("close"));

                            }

                             

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

                            magBrock.addEventListener(MouseEvent.CLICK,replaceSWF);

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

                            function replaceSWF(e:MouseEvent):void {

                                 dispatchEvent(new Event("swfs/printBrock01.swf"));

                            }

                             

                             

                             

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

                            magRacer.addEventListener(MouseEvent.CLICK,replaceSWF);

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

                            function replaceSWF(e:MouseEvent):void {

                                 dispatchEvent(new Event("swfs/printRacer01.swf"));

                            }

                            • 11. Re: Controlling multiple external swfs from main timeline
                              Ned Murphy CommunityMVP

                              I will not be able to help you with whatever relaxatraja provided though it is purported by relaxatraja to be clearly explaining what I was explaining (???).

                               

                              Your loadHandler function appears a bit confused.  For the code you show I don't see you assigning any event listeners for the events that will be dispatched in the loaded file.  As I showed earlier, your loadHandler function should be assigning a listener for the event that will be dispatched (modified slightly below). and if you have three different events being dispatched in the loaded file, then you'll do better to have three different listeners.  I show only one below for one of the events you dispatch...

                               

                               

                              function loadHandler (event:Event) {

                               

                                // assign a listener for the loaded file's goExit function's event

                                 MovieClip(event.currentTarget.content).addEventListener("close", event1Handler);

                               

                              // I can't make sense of what you have here

                                  removeChild(myClip); // why ?? - it hasn't been added yet as far as I can see - error 2007?

                                  var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf"); // the event.target is the Loader

                                  ldr.load(newSWFRequest); // this will load over the file you just finished loading??

                                  ldr.x = Xpos;

                                  ldr.y = Ypos;

                                  addChild(myClip);  // why?? it has nothing in it

                              }

                               

                              function event1Handler(e:Event):void {

                                  // do whatever is involved with what the "close" event is supposed to do

                                  trace("goExit function executed");

                              }

                               

                              As far as what you have in the loaded file, you appear to be trying to pass the files names as if they were parameters.  The file naming should be done at the receiving side for the event handler function for the event listener.  While you could create a custom event that include parameters that you can pass, that's another level of coding that is better left for a future experience.

                               

                              Study the following - understand it before you try to use it.

                               

                              Below is a link to some files I made for another posting that demonstrates what I have been explaining.  The neurope file is the main file.  Publish the netherlands file so that you have an swf to load for it, and then run the neurope file.  The file that gets loaded (netherlands) only has a button in it that when clicked dispatches an event for which the main file (neurope) has assigned a listener.  The main file displays a message when the button gets clicked in the loaded file.

                               

                              http://www.nedwebs.com/Flash/neurope.zip

                               

                              • 12. Re: Controlling multiple external swfs from main timeline
                                relaxatraja Community Member

                                Hi, I just tried to give some knowledge transferring and I dont know how much it really help out for your problem in detail. So please follow the ideas from Ned Murphy, bcoz he understood your problem clearly .

                                • 13. Re: Controlling multiple external swfs from main timeline
                                  ellenst Community Member

                                  Hi Ned, thanks for you patience.  Thanks for the file also, I have study it and I feel I understand it.  This feels like when I was learning spanish, I could read spanish, but writing it, or speaking it properly, was another level altogether! 

                                   

                                  I have tried to adjust your file to suit my needs.  The External SWF unloads when clicked, but i am getting an "Error #1009: Cannot access a property or method of a null object reference".  I have commented the details in the code below.  And I don't know if the load function is correct.

                                   

                                  I'm starting to feel like I'm imposing on you, Let me know if I am.  I hope I'm making progress.

                                   

                                  Here's the adjusted code, I have used my files as the loading SWF's

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

                                   

                                  stop();

                                  var loaderExtSWF:Loader = new Loader();

                                   

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

                                  function goNeth(e:Event=null):void {

                                  loaderExtSWF.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);

                                  loaderExtSWF.load(new URLRequest("swfs/printMagazines01a.swf"));    

                                  }

                                  // printMagazines01a.swf has two buttons inside a MovieClip that when click should

                                  // unload "printMagazines01a.swf" and load "printBrock01.swf", which in turn will unload itself and

                                  // load another SWF when click

                                   

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

                                  function loadHandler(e:Event):void

                                  {

                                  addChild(loaderExtSWF)

                                  MovieClip(e.currentTarget.content).addEventListener("magBrockTrigger", event1Handler);

                                  }

                                   

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

                                  function event1Handler(e:Event):void

                                  {

                                  //trace("button clicked in loaded swf");

                                  removeChild(loaderExtSWF)

                                  MovieClip(e.currentTarget.content).addEventListener("printBrock01Trigger", event2Handler);

                                  }

                                  // This works and unloads the "printMagazines01a.swf" but I get this error message -

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

                                  // at printMagazinesB_fla::MainTimeline/event1Handler()

                                  // at flash.events::EventDispatcher/dispatchEventFunction()

                                  // at flash.events::EventDispatcher/dispatchEvent()

                                  // at printMagazines01a_fla::Mag01_mc_1/disEvent()

                                   

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

                                  function event2Handler(e:Event):void

                                  {

                                  addChild(loaderExtSWF)

                                  }

                                  // I want "event2Handler" to load the new SWF, I don't know if this is correct

                                   

                                  goNeth();

                                   

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

                                  //This is the code I have inside "printMagazines01a.swf"

                                   

                                  stop();

                                   

                                  magBrock.addEventListener(MouseEvent.CLICK, disEvent);

                                  magRacer.addEventListener(MouseEvent.CLICK, disEvent);

                                  // these are the buttons inside "mag01_Mc" on the 1st frame of "printMagazines01a.swf"

                                   

                                  function disEvent(evt:MouseEvent):void {

                                  dispatchEvent(new Event("magBrockTrigger", true));

                                  }

                                   

                                  magBrock.addEventListener(MouseEvent.CLICK, disEvent02);

                                  magRacer.addEventListener(MouseEvent.CLICK, disEvent02);

                                   

                                  function disEvent02(evt:MouseEvent):void {

                                  dispatchEvent(new Event("printBrock01Trigger", true));

                                  }

                                  // This function is meant to load the next SWF "printBrock01.swf"

                                   

                                  • 14. Re: Controlling multiple external swfs from main timeline
                                    Ned Murphy CommunityMVP

                                    I think the problem lies here...

                                     

                                    function event1Handler(e:Event):void

                                    {

                                       removeChild(loaderExtSWF)

                                       MovieClip(e.currentTarget.content).addEventListener("printBrock01Trigger", event2Handler);

                                    }

                                     

                                    If you want to see why, try tracie(e.currentTarget) before and after the removeChild line.  Let your main file manage all of the loading and unloading, and remember, you do not want to assign that listener until after the file has been loaded.

                                     

                                    If I get the feeling that someone is imposing on me, which you have not been doing at all, I will let someone know and deny them the effort they are asking of me.  I am otherwise here to help and learn if I can.

                                    • 15. Re: Controlling multiple external swfs from main timeline
                                      Ned Murphy CommunityMVP

                                      And for the sake of adding something to your troubleshooting arsenal, here's a bit of cut and paste I usually offer when I see those magic numbers (1009)....

                                       

                                      The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....
                                       
                                      - is not in the display list
                                      - doesn't have an instance name (or the instance name is mispelled)
                                      - does not exist in the frame where that code is trying to talk to it
                                      - is animated into place but is not assigned instance names in every keyframe for it
                                      - is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
                                       
                                      If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

                                      • 16. Re: Controlling multiple external swfs from main timeline
                                        ellenst Community Member

                                        Hi Ned, I thought, after all your help, I'd let you know where I am with this problem.

                                         

                                        I've been trying to work this out in my spare time since the last post, but I still can't work out how to get the new swf to load.  I've given up and returned to my work around for the moment.

                                         

                                        I've realised I need to go back to basics and gain more understanding of AS3.  I'm working my way through "Essential AS3" by Colin Moock, I notice someone recommended it on the forum.  Hopefully, I will then be able to work out how to resolve this, if not I may yet still be in touch!

                                         

                                        Thanks for your help up till now.

                                         

                                        Cheers,

                                        Ellen