8 Replies Latest reply on Jan 26, 2008 1:20 AM by Ahsan Rathore

    How to load multiple SWFs through XML

    Ahsan Rathore
      Hello Friends,
      Dear, I am making a slideshow in which I want to add multiple SWF files (10 files) through XML. Actually I am poor in scripting that's why I need yours help.

      Actually I used the following code;

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

      var banner:XML = new XML();
      banner.ignoreWhite = true;
      banner.load("banner.xml");

      banner.onLoad = function(success) {
      var videos:Array = this.firstChild.childNodes;
      var aq = videos.length;
      for (var i:Number = aq; i > 0; i--) {
      loadMovie(videos .attributes.url, target_mc);
      }
      };
      ------------------------------------------------------------------------------------------ ---

      And in XML I used this format;

      ----------------------------------------------------------------------------------------- ----
      <?xml version="1.0" encoding="ISO-8859-1"?>

      <videos>
      <video url="a.swf" />
      <video url="b.swf" />
      </videos>
      ------------------------------------------------------------------------------------------

      In this case it only load 1 SWF movie and stopped. It will not play other movie. Will anyone please help me to solve this issue, that how can I load my multiple SFW files.
      It's urgent dear, If you guide me I'll be very thankful to you. Thanks a lot!
        • 1. Re: How to load multiple SWFs through XML
          Level 7
          AhsanAas,

          These few lines here are the problem:

          for (var i:Number = aq; i > 0; i--) {
          loadMovie(videos .attributes.url, target_mc);
          }

          Consider what you're asking this SWF to do: this for() loop runs its
          instructions in quick succession. Your XML indicates two additional SWFs,
          but it might be five (or ten, or dozens) -- doesn't matter. In all cases,
          you're loading every additional SWF file to the same target (they all load
          into target_mc). As soon as a new SWF is requested, it replaces whatever
          was in target_mc already.

          If you want all SWFs to show at once, you'll have to create a target for
          each of them. You could do this beforehand, but that would somewhat defeat
          the purpose of using XML in the first place (XML is supposed to give you
          flexibility in determining the external content without recompiling the main
          SWF; it's even more useful when the number of external SWFs can be
          determined by the XML). If you want each external SWF to be loaded into its
          own target, you could use the MovieClip.createEmptyMovieClip() method to
          produce these targets at runtime. In the same way you used the variable i
          in the for() loop (videos
          ) you could use it again to ensure unique
          instance names and depths for your targets:

          for (var i:Number = aq; i > 0; i--) {
          var target_mc = this.createEmptyMovieClip("container" + i, i);
          loadMovie(videos .attributes.url, target_mc);
          }

          In fact, you could even use i to reposition these new targets via the
          MovieClip._x property (or _y):

          for (var i:Number = aq; i > 0; i--) {
          var target_mc = this.createEmptyMovieClip("container" + i, i);
          mc._x = i * 100;
          loadMovie(videos
          .attributes.url, target_mc);
          }

          ... and so on. But you didn't say if you need all loaded SWFs present at
          once. Maybe you need them loaded sequentially?


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: How to load multiple SWFs through XML
            Ahsan Rathore Level 1
            Thanks a lot dear.. I am very happy to see your response. Thanks Again!
            By the way sorry, that I didn't clarify you that I need to run this SWFs sequentially. Is there a different solution in this case? If yes, then please reply. Thanks!
            • 3. How to load multiple SWFs through XML
              Ahsan Rathore Level 1
              Dear, your code is for loading SWFs at once in different targets. Will you please tell me the solution in which I load these SWFs sequentially in same target. Thanks!
              • 4. Re: How to load multiple SWFs through XML
                Level 7
                AhsanAas,

                > Dear, your code is for load SWFs at one in different
                > targets.

                Yes! I had hoped that was clear from my reply -- but I wasn't sure what
                you needed, which is why I asked about sequential loading at the end.

                > Will you please tell me the solution in which I load
                > theses SWFs sequentially in same target. Thanks!

                As with many endeavors in Flash (and most programming environments),
                there are several possible ways to accomplish your goal. Here's one
                approach:

                // In frame 1 of the main timeline ...
                var banner:XML = new XML();
                var videos:Array;
                var currentVideo:Number = 0;

                banner.ignoreWhite = true;
                banner.load("banner.xml");

                banner.onLoad = function(success:Boolean):Void {
                if (success) {
                videos = this.firstChild.childNodes;
                loadMovie(videos[currentVideo].attributes.url, target_mc);
                }
                };

                function loadNextVideo():Void {
                currentVideo++;
                if (currentVideo < videos.length) {
                loadMovie(videos[currentVideo].attributes.url, target_mc);
                }
                }

                The above lines would replace what you currently have. Note that in
                this case, the videos array is declared outside of any function, which
                scopes the variable to the main timeline (this means it is accessible to
                code outside of the function -- in your previous version, the videos array
                was only accessible inside your onLoad event handler function).

                Loading happens as before, but for sake of completeness, I've added the
                strong typing (:Boolean and :Void) that weren't there before. They're not
                strictly needed, but it's a good practice to get into the habit. Notice
                that the for() loop is now gone. In its place, the loadMovie() call simply
                loads the first video in the array -- first, because the value of
                currentVideo is zero.

                Finally, a custom function, loadNextVideo(), updates the value of
                currentVideo by one (++), then checks if that value is still within the
                range of the video array's length. If so, it calls the same loadMovie()
                function as before. This time, the value of currentVideo is 1, so the
                second video is loaded. Next time, that value will be 2, so the third video
                will be loaded. After that, the value will be 3 -- and, assuming there are
                only three videos, the value of currentVideo will no longer be less than
                videos.length, so the loadMovie() function won't be called again.

                The only "trick," now, is to call this loadNextVideo() function at the
                right time. This can be accomplished by putting a bit of keyframe code into
                the last frame of each SWF file that gets loaded:

                // In the last keyframe of each external SWF ...
                this._parent.loadNextVideo();

                Assuing target_mc is an immediate child of the main timeline, the
                expression, this.parent will point to the main timeline from the inside of
                each external SWF. With a proper reference to the main timeline, the
                loadNextVideo() function can be executed.


                David Stiller
                Contributor, How to Cheat in Flash CS3
                http://tinyurl.com/2cp6na
                "Luck is the residue of good design."


                • 5. How to load multiple SWFs through XML
                  Ahsan Rathore Level 1
                  Dear, Mr. Stiller thanks a lot for your help.. I achieved my goal... You are great.. Thanks a lot again..

                  1 more question if you don't mind? How can I use preloader function to load my external movies...What should I change in this script and where should i define it.
                  Please help... Thanks a lot again for your help... You have really won my heart.....
                  Here is the script....
                  • 7. Re: How to load multiple SWFs through XML
                    Level 7
                    AhsanAas,

                    > Dear, Mr. Stiller thanks a lot for your help.. I achieved my goal...

                    Super!

                    > 1 more question if you don't mind? How can I use preloader
                    > function to load my external movies...

                    The onEnterFrame approach you used is fine, but the MovieClipLoader
                    class is easier to use. Instead of loadMovie() in your complete event
                    handler, you would create an instance of MovieClipLoader, then use that
                    instance to invoke load(). Inside that load() call, you would specify
                    videos[currentVideo].attributes.url and target_mc, as before. The
                    difference is that your MovieClipLoader instance then offers events to
                    indicate when loading has completed. It's all much easier to use (less code
                    to write) than using an onEnterFrame handler to loop.

                    See if this gets you started:

                    http://www.quip.net/blog/2006/flash/how-to-tell-when-external-swf-loaded

                    It shows both approaches.


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 8. Re: How to load multiple SWFs through XML
                      Ahsan Rathore Level 1
                      Thanks a a lot Mr. Stiller, I have done it... Thanks...
                      You are really great..