5 Replies Latest reply on May 31, 2007 12:11 AM by OlandoCR

    Dealing with preloaders in Flasch CS3

    OlandoCR Level 1
      Dear all,

      I need your help to deal with preloaders in Flash CS3. After reading some comments in other forums, I still do not have clear how to proceed.

      This is the situation.

      1. I have a project (flash project) which loads dynamically movie clips (from now mc) to stage.
      2. Those mc have the linkage option checked in order to load them using addChild .
      3. The mc will be used in different times; I do not have to add them all at once.
      4. In my first approach the mc are in the library and I succeed loading them using this code:

      var classRef:Class = getDefinitionByName(nameMC) as Class;
      var instance:Object = new classRef();
      container.addChild(DisplayObject(instance));

      5. Now I want to add a preloader. According to previous readings, basically there are two ways to do it: 1. Putting all my mc in a frame inside my main swf. 2. Put them in a swf and load them dynamically.
      I tried the second one because I found in other forum how to load external swf (thanks senocular). This is the code:

      var request:URLRequest = new URLRequest("assetsContainer.swf");
      var myAssets:Loader = new Loader();

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

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

      myAssets.load(request);
      //addChild(myAssets);
      stop();

      So far everything is ok.

      My question is (sorry for the long introduction) how I handle the content of my loaded swf?

      How I use container.addChild(DisplayObject(instance)) now?

      And my other question is. As you can see in the previous code, I can add my loaded swf to stage using addChild(myAssets); but I do not want to do that. If I do not render the loaded swf, it will keep in memory? Is this a good approach?

      I will appreciate a lot, all help you can give me.
        • 1. Re: Dealing with preloaders is Flasch CS3
          kglad Adobe Community Professional & MVP
          1. after loading is complete you can reference myAssets to control your loaded swf.

          2. i have no idea what you want to do with container.addChild(DisplayObject(instance)). that has nothing to do with your topic.

          3. yes, it will keep in memory as long as myAssets exists (and you don't load another swf into it).

          4. is what a good approach to accomplish what?
          • 2. Re: Dealing with preloaders is Flasch CS3
            OlandoCR Level 1
            kglad, thanks for the fast response. Sorry if I did not explain myself well, it is difficult when you do not know the software and you speak a different language. I will try again.

            1. container.addChild(DisplayObject(instance)). In the current stage of my project I use this line to load my mc from the library to stage. With the proloader I still need to load the mc to a container. I supposed I get the mc from the loaded swf like I did from the library or I have to use a different approach?
            2. About to keep the loaded swf on memory, this is what I meant by "good approach to accomplish", I mean if is a good strategy to load a swf with my mc, and keep it in memory to use its content when I need.

            Thanks for your help


            • 3. Re: Dealing with preloaders is Flasch CS3
              kglad Adobe Community Professional & MVP
              1. a preloader has nothing to do with instantiating an object from your library. library movieclips are already downloaded into the user's computer when your swf starts.

              2. sure, that's fine.
              • 4. Re: Dealing with preloaders is Flasch CS3
                OlandoCR Level 1
                kglad, let me change the question. If I have the next code:

                var myFile:String = "assetsContainer.swf";
                var request:URLRequest=new URLRequest(myFile);
                var loader:Loader=new Loader();
                var myDispatcher:IEventDispatcher = loader.contentLoaderInfo;
                myDispatcher.addEventListener(Event.INIT, completed);
                loader.load(request);
                function completed(event:Event):void {
                var myMovie:MovieClip=event.currentTarget.content;
                }

                How I can take a specific “child" of myMovie and add it to a “container” to be render on stage? Something like:

                container.addChild(the child of myMovie).

                Thanks in advance

                • 5. Dealing with preloaders in Flasch CS3
                  OlandoCR Level 1
                  Ok, finally I was able to load a swf file with all my assets in put them in a container to be render. This is the code:

                  var request:URLRequest = new URLRequest("assetsContainer.swf");
                  var loader:Loader = new Loader();
                  var myMCList:MovieClip = new MovieClip();
                  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 Assets: "+percentLoaded+"%");
                  }
                  function loadComplete(event:Event):void {
                  myMCList= event.currentTarget.content;
                  trace("Assets Loaded");
                  gotoAndStop("starto")
                  t
                  }

                  loader.load(request);

                  Now, if want to populate a container with movie clips from my loaded swf I use this code:

                  var myMc:Object = new Object();
                  var container:MovieClip= new MovieClip();
                  myMc = myMCList.getChildByName(the name of my movie clip inside the loaded swf );
                  this.container.addChild(DisplayObject(myMc));
                  this.addChild(container);

                  So, I have all my assets in memory and I can add them to stage when I need.

                  What do you think? Makes sense?

                  Preloading code was taken from here: http://www.kirupa.com/forum/showthread.php?p=2112823#post2112823
                  senocular code