24 Replies Latest reply on Oct 23, 2007 3:28 PM by kglad

    onLoadComplete problem in preloader

    RobCS
      I'm trying to display a swf file, but only after it's 100% loaded. Even though the code below seems to request that the swf doesn't show until the load is "complete", when I publish the file and use the "simulate download" function, it displays the swf after only 72% has been loaded. The file size is about 1400KB, and it seems to display after about 1100KB are loaded. Any suggestions?



      on (release) {

      this._visible = false;
      _level0._visible = true;
      //_level0.main_menu._visible = false;



      var myMCL:MovieClipLoader = new MovieClipLoader();
      var myListener:Object = new Object();

      myMCL.addListener(myListener);

      myListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
      var preloadPercent:Number = Math.round((bytesLoaded/bytesTotal) * 100);
      _level5.progressBar.gotoAndStop(preloadPercent);
      }

      myListener.onLoadInit = function (target_mc:MovieClip) {
      _level5.progressBar._visible = true;
      }

      myListener.onLoadStart = function (target_mc:MovieClip) {
      _level5.progressBar._visible = true;
      }

      myListener.onLoadComplete = function(target_mc:MovieClip) {
      _level5.progressBar._visible = false;
      }

      _level0.myMCL.loadClip("bobsRoom.swf", 10);

      }
        • 1. Re: onLoadComplete problem in preloader
          kglad Adobe Community Professional & MVP
          you're not doing anything to force the swf to not show until loading is complete.
          • 2. Re: onLoadComplete problem in preloader
            RobCS Level 1
            such as?
            • 3. onLoadComplete problem in preloader
              Greg Dove Level 4
              your onLoadComplete doesn't do anything to the the target_mc clip's visibility property. I've never used MovieClipLoader to load into a level, always into a movieclip target (but I checked the docs and it looks like what you're doing is OK)...nor have I seen a level number higher than 9 before....so I don't know if this is exactly correct....

              You probably want something like:

              myListener.onLoadInit = function (target_mc:MovieClip) {
              _level5.progressBar._visible = true;
              _level10.stop();
              _level10._visible=false;
              //or perhaps target_mc in place of _level10
              }

              myListener.onLoadComplete = function(target_mc:MovieClip) {
              _level5.progressBar._visible = false;
              _level10.play();
              _level10._visible=true;
              }

              • 4. Re: onLoadComplete problem in preloader
                kglad Adobe Community Professional & MVP
                no, that's not going to work gwd. you need to assign the _visible property of _level10 to be false when loading starts and assign it to be true when you you want to display its contents.

                the problem with levels, is you can't access their properties until something is loaded into them. so, the op will (if they want to continue to use a target level) need to load an empty swf into _level10. assign its _alpha (because the _visible property is set to default when loading starts) to zero. then start loading bobsRoom.swf.

                in their onLoadInit() or anytime thereafter when they are ready to display bobsRoom.swf, they assign the _alpha to 100.
                • 5. Re: onLoadComplete problem in preloader
                  RobCS Level 1
                  Well, I dumped all references to levels and changed it to "target_mc", but it still wants to display long before it's fully loaded.

                  here's the code:

                  var myMCL:MovieClipLoader = new MovieClipLoader();
                  var myListener:Object = new Object();

                  myMCL.addListener(myListener);

                  myListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
                  var preloadPercent:Number = Math.round((bytesLoaded/bytesTotal) * 100);
                  progressBar.gotoAndStop(preloadPercent);
                  }

                  myListener.onLoadInit = function (target_mc:MovieClip) {
                  progressBar._visible = true;
                  target_mc.stop();
                  target_mc._visible=false;
                  //or perhaps target_mc in place of _level10
                  }

                  myListener.onLoadStart = function (target_mc:MovieClip) {
                  progressBar._visible = true;
                  }

                  myListener.onLoadComplete = function(target_mc:MovieClip) {
                  progressBar._visible = false;
                  target_mc.play();
                  target_mc._visible=true;
                  }

                  myMCL.loadClip("bobsRoom2.swf", 10);

                  }
                  • 6. Re: onLoadComplete problem in preloader
                    Greg Dove Level 4
                    kglad: thanks for pointing that out. I've never really needed to use levels.
                    Like I said earlier I wasn't too familiar with them.
                    • 7. Re: onLoadComplete problem in preloader
                      kglad Adobe Community Professional & MVP
                      you're still loading into _level10. change your loadClip's 2nd parameter if you want to load into a target movieclip. and don't use its _visible property (which is reset to default or true when loading starts): use its _alpha property.

                      and remove your onLoadComplete handler. you're just getting confused. use the onLoadInit handler to determine when your swf is completely loaded and ready for use.

                      and assign your target movieclip's _alpha to zero after you create a target movieclip (which does not appear to have been done).
                      • 8. Re: onLoadComplete problem in preloader
                        Greg Dove Level 4
                        kglad: onLoadInit is 'ready for use' (i.e can assign properties) but not necessarily 'completely loaded'. onLoadComplete follows that when 100% loaded which is what Robcs wanted I think.

                        • 9. Re: onLoadComplete problem in preloader
                          RobCS Level 1
                          GWD is right. I do want to wait until it's completely loaded, since there's a large "drain" at the beginning of the swf (if they click right away) and that causes it to stall. Maybe I should just toss the code out and start from scratch.
                          • 10. Re: onLoadComplete problem in preloader
                            kglad Adobe Community Professional & MVP
                            onLoadComplete executes first, then onLoadInit. so, if onLoadInit executes, you can be sure loading is complete.

                            there are many situations when using onLoadComplete is inadequate and there are only a few occasions when using onLoadComplete() is preferrable to onLoadInit().
                            • 11. onLoadComplete problem in preloader
                              Greg Dove Level 4
                              kglad: actually I think it depends on the type of clip. That is true for a single frame movieclip or an image but not for a multi-frame movieclip. onLoadComplete is basically bytesLoaded==bytesTotal. So for a preloader its the end of preloading.

                              onLoadInit is "Invoked when the actions on the first frame of the loaded clip have been executed" which will be before onLoadComplete for a larger, multiframe clip or after onLoadComplete for a single frame clip (or image).

                              EDIT: NO I'M COMPLETELY WRONG. kglad is correct.
                              I just tested it and you are correct kglad. But it doesn't make sense to me as the swf format is supposed to be 'streaming' so that the first frames can be playing/executing (ie. 'onLoadInit') before the swf is fully downloaded (i.e. 'onLoadComplete'). I can only assume that onLoadComplete is not bytesLoaded==bytesTotal. And yet my test showed that it was. I did this over a fast connection which may have complicated things and with a moderately sized swf with most content at the end of the timeline... but I did do it in the browser. I tend to always use onLoadInit anyway....but this doesn't make a whole lot of sense to me.

                              • 12. Re: onLoadComplete problem in preloader
                                RobCS Level 1
                                I appreciate the help guys, but now I'm a bit confused as to what the code should be. Can either of you clarify it for me?

                                thanks!
                                • 13. Re: onLoadComplete problem in preloader
                                  kglad Adobe Community Professional & MVP
                                  again:

                                  you're still loading into _level10. change your loadClip's 2nd parameter if you want to load into a target movieclip. and don't use its _visible property (which is reset to default or true when loading starts): use its _alpha property.

                                  and remove your onLoadComplete handler. you're just getting confused. use the onLoadInit handler to determine when your swf is completely loaded and ready for use.

                                  and assign your target movieclip's _alpha to zero after you create a target movieclip (which does not appear to have been done).
                                  • 14. Re: onLoadComplete problem in preloader
                                    Greg Dove Level 4
                                    RobCS: and please ignore what I've said and just listen to kglad. I apologize if I added to the confusion.
                                    • 15. Re: onLoadComplete problem in preloader
                                      RobCS Level 1
                                      Okay, I cleaned up the code and removed the level references, but it still doesn't work. (see code below). I should mention that the target movie clip, "container", resides in the main movie clip, which this one loads into in the first place.

                                      thanks.

                                      on (release) {

                                      var myMCL:MovieClipLoader = new MovieClipLoader();
                                      var myListener:Object = new Object();

                                      myMCL.addListener(myListener);

                                      myListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
                                      var preloadPercent:Number = Math.round((bytesLoaded/bytesTotal) * 100);
                                      progressBar.gotoAndStop(preloadPercent);
                                      }

                                      myListener.onLoadStart = function (target_mc:MovieClip) {
                                      progressBar._visible = true;
                                      }

                                      myListener.onLoadInit = function (target_mc:MovieClip) {
                                      progressBar._visible = false;
                                      target_mc.play();
                                      target_mc._alpha=100;
                                      }

                                      myMCL.loadClip("bobsRoom2.swf", container);

                                      }
                                      • 16. Re: onLoadComplete problem in preloader
                                        kglad Adobe Community Professional & MVP
                                        where's container created? is that code attached to a button or movieclip?

                                        you can use trace(container) below your loadClip() statement to confirm all's well if you think that above are non-issues. report the results of the trace.
                                        • 17. Re: onLoadComplete problem in preloader
                                          RobCS Level 1
                                          "container" is created as a movie clip in the "root" swf. I've adjusted the code in this swf to the below, but it still displays the clip "bobsRoom2.swf" before it's totally loaded. Also, the "progressBar" movieclip, also in the "root" swf, doesn't display.

                                          on (release) {

                                          var myMCL:MovieClipLoader = new MovieClipLoader();
                                          var myListener:Object = new Object();

                                          myMCL.addListener(myListener);

                                          myListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
                                          var preloadPercent:Number = Math.round((bytesLoaded/bytesTotal) * 100);
                                          _root.progressBar.gotoAndStop(preloadPercent);
                                          }

                                          myListener.onLoadStart = function (target_mc:MovieClip) {
                                          _root.progressBar._alpha = 100;
                                          target_mc.stop();
                                          target_mc._alpha = 0;
                                          }

                                          myListener.onLoadInit = function (target_mc:MovieClip) {
                                          _root.progressBar._alpha = 0;
                                          target_mc.play();
                                          target_mc._alpha = 100;
                                          }

                                          loadMovie("bobsRoom2.swf", _root.container);

                                          }
                                          • 18. Re: onLoadComplete problem in preloader
                                            RobCS Level 1
                                            Just to clarify, the code above is in the swf that loads into the "root" swf.
                                            • 19. Re: onLoadComplete problem in preloader
                                              RobCS Level 1
                                              I finally got it! Thanks for all your patience and help. The final code that got it to work properly was:

                                              on (release) {

                                              var myMCL:MovieClipLoader = new MovieClipLoader();
                                              var myListener:Object = new Object();

                                              myMCL.addListener(myListener);

                                              myListener.onLoadProgress = function(target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {
                                              var preloadPercent:Number = Math.round((bytesLoaded/bytesTotal) * 100);
                                              _root.progressBar.gotoAndStop(preloadPercent);
                                              }

                                              myListener.onLoadStart = function (target_mc:MovieClip) {
                                              _root.progressBar._visible = true;
                                              _root.progressBar.play();
                                              target_mc.stop();
                                              target_mc._alpha = 0;
                                              }

                                              myListener.onLoadInit = function (target_mc:MovieClip) {
                                              _root.progressBar._alpha = 0;
                                              target_mc.play();
                                              target_mc._alpha = 100;
                                              }

                                              myMCL.loadClip("bobsRoom.swf", _root.container);

                                              }
                                              • 20. Re: onLoadComplete problem in preloader
                                                kglad Adobe Community Professional & MVP
                                                you're welcome.
                                                • 21. Re: onLoadComplete problem in preloader
                                                  RobCS Level 1
                                                  Now that I've got the swf and preloader to load properly, i want to have it skip the perload and load activities when the button is clicked later, and just change the visibility/alpha to display the already loaded swf. I thought it might require an if/else statement, but wondered what the best approach was.
                                                  • 22. Re: onLoadComplete problem in preloader
                                                    kglad Adobe Community Professional & MVP
                                                    you can use an if-statement on the first line in your on(release) handler that checks the bytesLoaded() or _totalframes to determine if loading has already occurred and, if so, toggle the _alpha or _visible property.
                                                    • 23. Re: onLoadComplete problem in preloader
                                                      RobCS Level 1
                                                      And this if statment in the button's "on(release)" will be able to reference the swf file that is now loaded in the "root" swf?
                                                      • 24. Re: onLoadComplete problem in preloader
                                                        kglad Adobe Community Professional & MVP
                                                        you'll use container (if you have a true button) or _parent.container (if that's a movieclip button) to reference the swf that's been loaded into container.