6 Replies Latest reply on Jun 11, 2007 2:34 PM by SymTsb

    How to preload using AS 3.0?

      Hi!

      I am using ActionScript 3.0 with Flash 9.

      I have asked this in many diferent places without any answer yet.Hopefully I will get my ansewers here.
      I have this code to preload external swf files :
      var request:URLRequest = new URLRequest("content.swf");
      var loader:Loader = new Loader();

      loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

      function loadProgress(event:ProgressEvent):void {
      var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
      percentLoaded = Math.round(percentLoaded * 100);
      trace("Loading: "+percentLoaded+"%");
      }
      function loadComplete(event:Event):void {
      trace("Complete");
      }

      loader.load(request);
      addChild(loader);

      I want to make a class so I can use it for many swf files(although I can put it in a function and still use it many times) and I came up with this code which I dont know if it is right:

      package
      {
      import flash.display.MovieClip;
      import flash.display.*;
      import flash.text.TextField;
      import flash.net.URLRequest;
      import flash.events.Event;
      import flash.events.ProgressEvent;

      public class Carregador extends MovieClip
      {

      public var _Barra : MovieClip = new MovieClip();
      public var _Texto : TextField = new TextField();
      public var _Palco : MovieClip = new MovieClip();
      public var _Movie : String;
      public var loader:Loader = new Loader();

      public function set Palco(value:MovieClip):void
      {
      _Palco = value;
      }

      public function set Barra(value:MovieClip):void
      {
      _Barra = value;
      }

      public function set Texto(value:TextField):void
      {
      _Texto = value;
      }

      public function set Movie(value:String):void
      {
      _Movie = value;
      }

      public function loadSwf():void
      {
      var request:URLRequest = new URLRequest(_Movie);

      loader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
      loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

      loader.load(request);
      addChild(loader);

      }

      private function showPreloader(evt:Event):void
      {
      addChild(_Barra);
      }

      private function loadProgress(event:ProgressEvent):void
      {
      var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
      percentLoaded = Math.round(percentLoaded * 100);
      _Barra.width = percentLoaded;
      }

      private function loadComplete(event:Event):void
      {
      removeChild(_Barra);
      _Palco.addChild(_Movie);
      }

      }
      }

      Can You guys guide me throught the steps to make a class to preload swf files into an empty movie clip in my movie?

      thank you


        • 1. Re: How to preload using AS 3.0?
          kglad Adobe Community Professional & MVP
          in as3 you don't load into a movieclip, you use a loader class member as the target of your load and then you add the loader to your display list. if you want something to display on-stage during the load process, you need to add that to the display list, too.

          so, unless Carregador is in the display list (ie, it's your document class or you add an instance of it to the display list in your fla), nothing will display.
          • 2. Re: How to preload using AS 3.0?
            Level 1
            How would I do if I wanted to load a swf file into the right side of my movie like I did with AS2.0 and empty movie clips?
            and I want to show an animation before too..

            thank you
            • 3. Re: How to preload using AS 3.0?
              kglad Adobe Community Professional & MVP
              you can still use as2. just change your publish settings.

              there's no reason to use as3 unless you need the performance benefit, you are using some feature of as3 that wan't available in as2 or you want to learn as3.
              • 4. How to preload using AS 3.0?
                SymTsb Level 2
                The following package and class are in use on my site so you can see what it does.... http://www.sd-dezign.com
                I am using this exact code to preload a jpeg file (could be any jpg, progressive jpg, gif, png or swf). I just wrote it like 2 days ago. Hope this helps you out.

                This is the most basic of preloaders coded in AS3 and part of an external package.

                To use the package, it will need to be inside a folder called cloader in the same area as the FLA file that uses it.

                The second part is importing the package into the FLA and then calling it with the necessary arguement which is the file you want to load.

                The third and final part is to add the preloader to the display list.

                Putting it all together, it would look something like this....

                • 5. Re: How to preload using AS 3.0?
                  Level 1
                  Hi!

                  First thank you guys for all the answers. I do want to learn AS 3.0 thats why I am struggling with this preloader thing.

                  Thank you SymTsb for the help and I wanted to preload the main movie first showing a bar and a text field showing the percentage and as soon as my movie gets loaded I want to montionly remove the bar and start my intro.And I also wanted to use the code to load external swf Files showing some animation during the progress bar.
                  How would I get this done with AS 3.0?Should I make a class or create a function? I use tween motions for the animation.

                  Thank you...
                  • 6. Re: How to preload using AS 3.0?
                    SymTsb Level 2
                    As I stated before, my example is a bare bones. The text issue is already solved and present in the code I provided. You'd just need to modify it so that you can make it look like you want.

                    Doing the loading bar is a matter of creating a box and animating its xscale from 0 - 100%. Doing this will require the Tween class. Read up on this stuff in the Flash Help.