28 Replies Latest reply on Mar 21, 2011 1:05 PM by TZheng

    how to add preloader to an existing swf??

    pcman2020

      how do i add a preloader to an already made swf?

      also, do i have the preloader open up in seperate html file.

      don't want to buy any programs to do this.

      i did try it by just changing the file names to preloader file name to load the swf.

      but it push all the content over to the right side of the screen.

        • 1. Re: how to add preloader to an existing swf??
          Harry Kunz Level 4

          You can try use my preloader i uploaded in thread http://forums.adobe.com/thread/565379. Just change the dimensions of my fla to that of your dimensions and change the file path in frame 1. This will be the file that you embed in your html, then it will load your swf file and give control to it.

          • 2. Re: how to add preloader to an existing swf??
            pcman2020 Level 1

            not sure if that is gonna help me. i dont know much about flash. alittle here and there.

             

            is that an external preloader. thinking that is what i would prefer, unless i still have to add code to the main movie yet for it to work.

            • 3. Re: how to add preloader to an existing swf??
              Harry Kunz Level 4

              Here i posted a sample HTML file that embeds the SwfLoader.swf (the preloader) which in turn loads YourFile.swf which will be your swf file. If you want to use another another name for YourFile.swf just rename it and update SwfLoader.fla's frame 1 with that name and export it to swf.

              • 4. Re: how to add preloader to an existing swf??
                pcman2020 Level 1

                it still pushes the whole movie to the right side of screen and enlarges it

                do i need to make the preloader swf the same dimensions as my movie.

                • 5. Re: how to add preloader to an existing swf??
                  Harry Kunz Level 4

                  Yes set the dimensions the same as your movie. Do you have the fla of you movie? are you using any fscommands in it?

                  • 6. Re: how to add preloader to an existing swf??
                    pcman2020 Level 1

                    i got it it was dimensions all the time lol


                    • 8. Re: how to add preloader to an existing swf??
                      pcman2020 Level 1

                      it is working now. but one question about the js code i didnt change

                      the dimensions for this part

                       

                       

                      <script type="text/javascript">

                       

                          // <![CDATA[

                       

                          var so = new SWFObject("SwfLoader.swf", "myMovie", "550", "400", "8");

                       

                      so.addParam("wmode", "transparent", "middle");

                       

                      so.write("inner");

                       

                          // ]]>


                      </script>

                       

                      and seems like it didnt make a difference.

                      • 9. Re: how to add preloader to an existing swf??
                        Harry Kunz Level 4

                        Oh yeah do change that too. Hmmm... maybe you don't need that code at all. If it works without it, then just comment it out with <!--  -->

                        • 10. new issue Re: how to add preloader to an existing swf??
                          pcman2020 Level 1

                          new issue

                           

                          preloader works great.

                           

                          but the flash file starts playing before the preloader is finished 100%.

                           

                          can i delay the the movie till the preloader is finished.

                          • 11. Re: new issue Re: how to add preloader to an existing swf??
                            Harry Kunz Level 4

                            place a stop() on frame 1 of your external movie. The preloader will call the play() event for it after it loaded your swf file.

                            • 12. Re: how to add preloader to an existing swf??
                              pcman2020 Level 1

                              Doesn't seem to make a difference on movie starting. Movie still starts before preloader reaches 100%.

                              • 13. Re: how to add preloader to an existing swf??
                                Harry Kunz Level 4

                                Did you verify that your main SWF really did stop at frame 1? Play your main SWF on your PC, did it stop on frame 1? If it didn't then it's your main SWF code overriding the stop.

                                • 14. Re: how to add preloader to an existing swf??
                                  pcman2020 Level 1

                                  It did stop, but may be it has something to do with code in my main HTML page.

                                  • 15. Re: how to add preloader to an existing swf??
                                    pcman2020 Level 1

                                    Ok still verifying that it is not stopping the way I expect. It starts before loader reaches 100%, the funny thing is though, I can see it faded in the background. Wonder if it has anything to do with code in the SWFLOADER.swf file. Here's the code:

                                     

                                    import flash.geom.*


                                    import flash.display.*

                                     

                                    var loadurl:String = "nebphotoflash/new_intro4.swf";
                                    var nDepth:Number = 0;
                                    var nWidth:Number = 200;
                                    var nHeight:Number = 20;
                                    var nPadding:Number = 3;
                                    var cLoader:MovieClipLoader = new MovieClipLoader();
                                    var oListener:Object = {onLoadInit:onContentLoaded};
                                    var mcLoader:MovieClip = this.createEmptyMovieClip("Loader_MC", 0);
                                    var mcContent:MovieClip = this.createEmptyMovieClip("Content_MC", 1);
                                    var mcLoadBarBg:MovieClip = mcLoader.createEmptyMovieClip("LoadBarBg_MC", nDepth++);
                                    var mcLoadBar:MovieClip = null; //Duplicated later with mcLoadBarBg
                                    var txtPercLoad:TextField = null; //Create after duplication
                                    var cMatrix:Matrix = new Matrix();
                                    mcLoader._alpha = 0;
                                    cMatrix.createGradientBox(nWidth, nHeight, 0, nPadding, nPadding);
                                    cLoader.addListener(oListener);
                                    mcLoader.lineStyle(1, 0x000066, 100);
                                    DrawRect(mcLoader, 0, 0, nWidth, nHeight);
                                    mcLoadBarBg.lineStyle(1, 0x0000FF, 0);
                                    mcLoadBarBg.beginGradientFill("linear", [0x006699, 0x0066FF], [100,100], [0, 255], cMatrix, SpreadMethod.PAD);
                                    DrawRect(mcLoadBarBg, 0, 0, nWidth - nPadding*2, nHeight - nPadding*2);
                                    mcLoadBarBg.endFill();
                                    mcLoadBar = mcLoadBarBg.duplicateMovieClip("LoadBar_MC", nDepth++);
                                    txtPercLoad = mcLoader.createTextField("PercLoad_TXT", nDepth++, 0, 0, nWidth, nHeight);
                                    mcLoadBar._alpha = 80;
                                    mcLoadBarBg._alpha = 30;
                                    Translate(mcTextMask, nPadding, nPadding);
                                    Translate(mcLoadBarBg, nPadding, nPadding);
                                    Translate(mcLoadBar, nPadding, nPadding);
                                    mcLoadBar._xscale = 0;
                                    mcContent._alpha = 0;
                                    mcContent._lockroot = true;
                                    mcLoader._x = Stage.width/2 - mcLoader._width/2;
                                    mcLoader._y = Stage.height/2 - mcLoader._height/2;
                                    txtPercLoad._x = mcLoader._width/2 - txtPercLoad._width/2;
                                    txtPercLoad._y = mcLoader._height/2 - txtPercLoad._height/2;
                                    SetTextFormat(txtPercLoad, "0%");
                                    mcLoader._alpha = 100;
                                    cLoader.loadClip(loadurl, mcContent);

                                     

                                    _root.onEnterFrame = function()
                                    {
                                       var nBytesLoaded:Number = mcContent.getBytesLoaded();
                                       var nBytesTotal:Number = mcContent.getBytesTotal();
                                       var nPercLoaded:Number = Math.round(nBytesLoaded / nBytesTotal * 100);
                                       if(nPercLoaded > 0)
                                       {
                                          SetTextFormat(txtPercLoad, nPercLoaded.toString() + "%");
                                          mcLoadBar._xscale = nPercLoaded;
                                       }
                                    }

                                     

                                    function onContentLoaded(Void):Void
                                    {
                                       //trace(_root + "::onContentLoaded");
                                       SetTextFormat(txtPercLoad, "100%");
                                       cLoader.removeListener(oListener);
                                       delete _root.onEnterFrame;
                                       delete oListener;
                                       delete cLoader;
                                       _root.onEnterFrame = function()
                                       {
                                          //trace(_root + "::onContentLoaded::_root.onEnterFrame");
                                          var nInc:Number = 5;
                                          mcLoader._alpha -= nInc;
                                          mcContent._alpha += nInc;
                                          if(mcLoader._alpha <= 0) startLoadedContent();
                                       }
                                    }

                                     

                                    function startLoadedContent(Void):Void
                                    {
                                       delete _root.onEnterFrame;
                                       mcLoader.removeMovieClip();
                                       mcContent._alpha = 100;
                                       mcContent.play();
                                    }

                                     

                                    function DrawRect(mc:MovieClip, nX:Number, nY:Number, nW:Number, nH:Number, nR:Number)
                                    {
                                       trace("DrawRect in: " + mc);
                                       if(nR == undefined) nR = 6;
                                       mc.moveTo(nX+nR,nY);
                                       mc.lineTo(nX+nW-nR,nY);
                                       mc.curveTo(nX+nW,nY,nX+nW,nY+nR);
                                       mc.lineTo(nX+nW,nY+nH-nR);
                                       mc.curveTo(nX+nW,nY+nH,nX+nW-nR,nY+nH);
                                       mc.lineTo(nX+nR,nY+nH);
                                       mc.curveTo(nX,nY+nH,nX,nY+nH-nR);
                                       mc.lineTo(nX,nY+nR);
                                       mc.curveTo(nX,nY,nX+nR,nY);
                                    }

                                     

                                    function SetTextFormat(txtField:TextField, sText:String)
                                    {
                                       var txtFmt:TextFormat = new TextFormat();
                                       sText = "Loading... " + sText;
                                       txtFmt.font = "Verdana";
                                       txtFmt.align = "center";
                                       txtFmt.size = 11;
                                       txtFmt.color = 0x000066;
                                       txtFmt.bold = true;
                                       txtField.selectable = false;
                                       txtField.text = sText;
                                       txtField.setTextFormat(txtFmt);
                                       txtFmt = null;
                                    }

                                     

                                    function Translate(mc:MovieClip, nX:Number, nY:Number):Void
                                    {
                                       mc._x = nX;
                                       mc._y = nY;
                                    }
                                    • 16. Re: how to add preloader to an existing swf??
                                      Harry Kunz Level 4

                                      Hmmm.. i doubt it is the SwfLoader because the only time it calls play is in function:

                                       

                                      function startLoadedContent(Void):Void

                                      {

                                         delete _root.onEnterFrame;

                                         mcLoader.removeMovieClip();

                                         mcContent._alpha = 100;

                                         mcContent.play();

                                      }

                                       

                                      There are compiler errors in your SwfLoader. Did you edit it? First error is at

                                       

                                      1. var oListener:Object = ; //Should be var oListener:Object = new Object();

                                      2. mcLoadBarBg.beginGradientFill("linear", 0x006699, 0x0066FF, 100,100, 0, 255, cMatrix, SpreadMethod.PAD); //You removed the square brackets from 0x006699 & 0x0066FF and 100 & 100 and 0 & 255 which was originally mcLoadBarBg.beginGradientFill("linear", [0x006699, 0x0066FF], [100,100], [0, 255], cMatrix, SpreadMethod.PAD); //The 2nd,3rd,4th parameters are arrays of colors, you just passed a number instead.

                                      • 17. Re: how to add preloader to an existing swf??
                                        pcman2020 Level 1

                                        NO, when i copied over to the forum it took stuff out for some reason, but if you look at again the stuff left out is there.

                                        • 18. Re: how to add preloader to an existing swf??
                                          pcman2020 Level 1

                                          how about this part

                                           

                                          function onContentLoaded(Void):Void

                                           

                                          {

                                           

                                             //trace(_root + "::onContentLoaded");

                                           

                                             SetTextFormat(txtPercLoad, "100%");

                                           

                                             cLoader.removeListener(oListener);

                                           

                                             delete _root.onEnterFrame;

                                           

                                             delete oListener;

                                           

                                             delete cLoader;

                                           

                                             _root.onEnterFrame = function()

                                           

                                             {

                                           

                                                //trace(_root + "::onContentLoaded::_root.onEnterFrame");

                                           

                                                var nInc:Number = 5;

                                           

                                                mcLoader._alpha -= nInc;

                                           

                                                mcContent._alpha += nInc;

                                           

                                                if(mcLoader._alpha <= 0) startLoadedContent();

                                           

                                             }

                                           

                                          }

                                           

                                          the mcLoader._alpha <=0

                                           

                                          should that be 100

                                          • 19. Re: how to add preloader to an existing swf??
                                            Harry Kunz Level 4

                                            This shouldn't be the problem because onContentLoaded is only called after your SWF file is fully loaded via the loader's listener object so there is no way it can reach there before loading has finished... What is your frame rate? The alpha is just used for smooth transitioning. You could even just substitute the block below with immediately calling startLoadedContent(). The difference is just that you don't have the fading transition.

                                             

                                            _root.onEnterFrame = function()
                                               {
                                                  //trace(_root + "::onContentLoaded::_root.onEnterFrame");
                                                  var nInc:Number = 5;
                                                  mcLoader._alpha -= nInc;
                                                  mcContent._alpha += nInc;
                                                  if(mcLoader._alpha <= 0) startLoadedContent();
                                               }

                                             

                                            I'm not sure what exactly the problem is.... nobody had had any problems with my preloader..

                                            • 20. Re: how to add preloader to an existing swf??
                                              pcman2020 Level 1

                                              The problem is the preloader is playing the content before the preloader is finished. And you can see the content behind the preloader but extremely faded. On some level I assume that is how the preloader works.

                                              Even with the stop(); in the main movie, it still starts before the preloader is finished.

                                              Might want to try it with a big file and you will see what I am talking about.

                                              There is no code in the main movie other then the stop();  that you told me to put in there.

                                              The other thing is i am using cs4, not sure if that makes any difference.

                                              • 21. Re: how to add preloader to an existing swf??
                                                Harry Kunz Level 4

                                                I tried using a 6MB file and i have no problems. See the loading in http://www.skinzoneskincare.com/MyTest/SwfLoader.swf. It starts animation after the content is fully 100% loaded.

                                                • 22. Re: how to add preloader to an existing swf??
                                                  pcman2020 Level 1

                                                  http://www.nebulaphotography.com/index2.html

                                                   

                                                  try mine and see if it does same thing

                                                   

                                                  one other thing to i do have it at a lower frame rate 15-18fps

                                                  • 23. Re: how to add preloader to an existing swf??
                                                    Harry Kunz Level 4

                                                    The preloader works as expected.. It's fine and starts animating with the picture of a kid after preloader reaches 100%. The movieclip holder of your content though did not get _alpha applied to it that's why you see it in the background while it is being loaded but it is at frame 1 and not playing. I set the alpha of the content holder to zero and it then fades in after load is complete to reveal your content smoothly. So the problem is just the alpha not getting applied to your loaded content while it was loading probably because you used the alpha in another way or its children. Try using mcContent._visible = false instead of mcContent._alpha = 0 at line 34. Then you have to remove the fading code

                                                     

                                                     

                                                    _root.onEnterFrame = function()

                                                       {

                                                          //trace(_root + "::onContentLoaded::_root.onEnterFrame");

                                                          var nInc:Number = 5;

                                                          mcLoader._alpha -= nInc;

                                                          mcContent._alpha += nInc;

                                                          if(mcLoader._alpha <= 0) startLoadedContent();

                                                    }

                                                     

                                                     

                                                    and replace it with immediately calling startLoadedContent() and setting mcContent._visible = true within startLoadedContent function

                                                    • 24. Re: how to add preloader to an existing swf??
                                                      pcman2020 Level 1

                                                      starts with fountain in the background. and did clear up the faded. but it still not starting at 100%.

                                                      starts at about 20%-50%. unless its my computer. or could it be that i am using flash CS4.

                                                      • 25. Re: how to add preloader to an existing swf??
                                                        Harry Kunz Level 4

                                                        You must have a movieclip that is on frame 1 with a child timeline that you did not stop. Can i see the fla file?

                                                        • 26. Re: how to add preloader to an existing swf??
                                                          pcman2020 Level 1

                                                          sry fla is to big

                                                          • 27. Re: how to add preloader to an existing swf??
                                                            Harry Kunz Level 4

                                                            I have an idea. Insert a blank keyframe at frame 1 and place a stop. Then all your content that follows shall be in keyframe 2. That way you are sure that the content is not seen until load is complete and the play is invoked.

                                                            1 person found this helpful
                                                            • 28. Re: how to add preloader to an existing swf??
                                                              TZheng

                                                              Hi Harry Kunz,

                                                               

                                                              I've tried your preloader for my .swf files. It's really useful. However, there are problems with some certain flash programs I have made. I'm using ActionScript 2. For example:

                                                               

                                                              1. some flash have very low frame rate, e.g. 0.5 fps. They are slide shows. Because the frame rates of the preloader and the flash program have a big difference, it's hard to make them work properly.

                                                               

                                                              2. Whenever my programs involves codes to embed a sound file (usually in frame 2 after the user press the "Enter" button in frame 1), with the preloader, the .swf would stop wherever the sound file is without playing the sound or doing anything.

                                                               

                                                              example code on the frame:

                                                               

                                                              voice = new Sound();

                                                              voice.attachSound("w1.mp3");

                                                              voice.onSoundComplete=function(){

                                                                   gotoAndStop(3);

                                                              }

                                                              voice.start();

                                                               

                                                              // In this case, the program won't play w1.mp3, and won't go to frame 3. But without the preloader, it works.

                                                               

                                                              These are the two biggest problems I've got so far with preloaders (I believe it's not just your preloader). I'm new to Flash programming, so I believe it's sth. I have to work around but I'm not sure how. Could you give me any advice?

                                                               

                                                              Thank you very much.