11 Replies Latest reply on Feb 8, 2011 12:30 PM by kglad

    Loader Q

    beno12345

      Hi;

      I have the following code in a class:

       

                  for (i; i < 7; ++i)
                  {
                      var img:Images = new Images(bkgnd_imgs[i], 1006,523, 0, 0);
                      img.x = 1006 * i;
                      img_container.addChild(img);
                  }

       

      This calls the following class:

       

      package  {
          import flash.display.MovieClip;
          import flash.display.Sprite;
          import flash.display.Bitmap;
          import flash.display.BitmapData;
          import flash.filters.*;
          import flash.filters.BitmapFilterQuality;
          import flash.net.URLRequest;
          import flash.net.URLLoader;
          import flash.display.Loader;
          import flash.display.LoaderInfo;
          import flash.display.DisplayObject;
          import flash.events.Event;
          import flash.events.IOErrorEvent;
         
          public class Images extends MovieClip
          {
              private var parent_container:Sprite = new Sprite();
              private var _path:String = new String("path");
              private var _myWidth:Number = new Number(20);
              private var _myHeight:Number = new Number(20);
              private var _myX:Number = new Number(20);
              private var _myY:Number = new Number(20);
              private var _myURL:String = new String("url");

       

              public function Images(path:String, myWidth:uint, myHeight:uint, myX:uint, myY:uint, myURL:String="")
              {
                  _path = path;
                  _myURL = myURL;
                  _myWidth = myWidth;
                  _myHeight = myHeight;
                  _myX = myX;
                  _myY = myY;
                  LoadImage();
              }
             
              function LoadImage():void
              {
                  parent_container = new Sprite();
                  addChild(parent_container)
                  var req:URLRequest = new URLRequest(_path);
                  var loader:Loader = new Loader();
                  loader.load(req);
                  loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);         
                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadedImage);
              }

       

              function LoadedImage(e:Event):void
              {
                  var loaderInfo:LoaderInfo = e.target as LoaderInfo;
                  var displayObject:DisplayObject = loaderInfo.content;
                  displayObject.width = _myWidth;
                  displayObject.height = _myHeight;
                  parent_container.addChild(displayObject);
                  parent_container.x = _myX;
                  parent_container.y = _myY;
              }
             
              function ioErrorHandler(event:IOErrorEvent):void {
                  trace("ioErrorHandler: " + event);
              }
          }
      }

       

      The problem is that it loads all the images at once. What I would like to do is preserve my "Images" class while somehow sending a flag to the calling class that the image has successfully loaded. Then I could rewrite my loop so that when the given image has loaded, to move on to loading the next one. If I were writing this in Python, I'd end with something like this:

       

      return img, flag

       

      How do I do this in AS3?

      TIA,

      Beno

        • 1. Re: Loader Q
          kglad Adobe Community Professional & MVP

          dispatch an event from your Images class when loading is complete.   start loading your first and add a listener to check when loading is complete.  when it's complete, start loading the next and add a listener.  etc until the last is loaded.

          • 2. Re: Loader Q
            beno12345 Level 1

            From googling, I got the following:

             

                import flash.events.IEventDispatcher;
                import flash.events.EventDispatcher;
                import flash.events.Event;

             

                public class Images extends MovieClip implements IEventDispatcher
                {
                    public var myDispatcher:IEventDispatcher= new EventDispatcher();

            ...

                    function LoadedImage(e:Event):void
                    {
                        myDispatcher.addEventListener(Event.COMPLETE, ImDone);
            ...

                    public function ImDone():void
                    {
                        trace("I'm done!");
                    }

             

            However, this never traces even though the event completes (er...I think). Please advise.

            TIA,

            Beno

            • 3. Re: Loader Q
              kglad Adobe Community Professional & MVP
              var index:int=0;
              var indexLimit:int=7;
              loadNextF();


              function loadNextF():void{
              if(index<indexLimit){

                              var img:Images = new Images(bkgnd_imgs[index], 1006,523, 0, 0);

              img.addEventListener("loadcomplete",loadNextF);
                              img.x = 1006 * index;
                              img_container.addChild(img);

              index++;
              }

              }

               

              //////////////////////////////

               

              package  {
                  import flash.display.MovieClip;
                  import flash.display.Sprite;
                  import flash.display.Bitmap;
                  import flash.display.BitmapData;
                  import flash.filters.*;
                  import flash.filters.BitmapFilterQuality;
                  import flash.net.URLRequest;
                  import flash.net.URLLoader;
                  import flash.display.Loader;
                  import flash.display.LoaderInfo;
                  import flash.display.DisplayObject;
                  import flash.events.Event;
                  import flash.events.IOErrorEvent;
                 
                  public class Images extends MovieClip
                  {
                      private var parent_container:Sprite = new Sprite();
                      private var _path:String = new String("path");
                      private var _myWidth:Number = new Number(20);
                      private var _myHeight:Number = new Number(20);
                      private var _myX:Number = new Number(20);
                      private var _myY:Number = new Number(20);
                      private var _myURL:String = new String("url");

               

                      public function Images(path:String, myWidth:uint, myHeight:uint, myX:uint, myY:uint, myURL:String="")
                      {
                          _path = path;
                          _myURL = myURL;
                          _myWidth = myWidth;
                          _myHeight = myHeight;
                          _myX = myX;
                          _myY = myY;
                          LoadImage();
                      }
                     
                      function LoadImage():void
                      {
                          parent_container = new Sprite();
                          addChild(parent_container)
                          var req:URLRequest = new URLRequest(_path);
                          var loader:Loader = new Loader();
                          loader.load(req);
                          loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);         
                          loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadedImage);
                      }

               

                      function LoadedImage(e:Event):void
                      {
                          var loaderInfo:LoaderInfo = e.target as LoaderInfo;
                          var displayObject:DisplayObject = loaderInfo.content;
                          displayObject.width = _myWidth;
                          displayObject.height = _myHeight;
                          parent_container.addChild(displayObject);
                          parent_container.x = _myX;
                          parent_container.y = _myY;

              dispatchEvent(new Event("loadcomplete"));
                      }
                     
                      function ioErrorHandler(event:IOErrorEvent):void {
                          trace("ioErrorHandler: " + event);
                      }
                  }
              }


              • 4. Re: Loader Q
                beno12345 Level 1

                Strange. I got this error:

                 

                ArgumentError: Error #1063: Argument count mismatch on Maya2012Main/BackgroundImages(). Expected 0, got 1.
                    at flash.events::EventDispatcher/dispatchEventFunction()
                    at flash.events::EventDispatcher/dispatchEvent()
                    at Images/LoadedImage()

                 

                The event successfully fired once from Images.as and, as the error states, got hung up in Maya2012Main.as at the BackgroundImages fn (which you called LoadNextF). But I copied it just like you said (just changed the fn name):

                 

                                img.addEventListener("loadcomplete",BackgroundImages);

                 

                What gives?

                TIA,

                Beno

                • 5. Re: Loader Q
                  kglad Adobe Community Professional & MVP

                  my error.   use:

                   

                   

                   

                  var index:int=0;
                  var indexLimit:int=7;
                  loadNextF(null);


                  function loadNextF(e:Event):void{
                  if(index<indexLimit){

                                  var img:Images = new Images(bkgnd_imgs[index], 1006,523, 0, 0);

                  img.addEventListener("loadcomplete",loadNextF);
                                  img.x = 1006 * index;
                                  img_container.addChild(img);

                  index++;
                  }

                  }

                   

                  //////////////////////////////

                   

                  package  {
                      import flash.display.MovieClip;
                      import flash.display.Sprite;
                      import flash.display.Bitmap;
                      import flash.display.BitmapData;
                      import flash.filters.*;
                      import flash.filters.BitmapFilterQuality;
                      import flash.net.URLRequest;
                      import flash.net.URLLoader;
                      import flash.display.Loader;
                      import flash.display.LoaderInfo;
                      import flash.display.DisplayObject;
                      import flash.events.Event;
                      import flash.events.IOErrorEvent;
                     
                      public class Images extends MovieClip
                      {
                          private var parent_container:Sprite = new Sprite();
                          private var _path:String = new String("path");
                          private var _myWidth:Number = new Number(20);
                          private var _myHeight:Number = new Number(20);
                          private var _myX:Number = new Number(20);
                          private var _myY:Number = new Number(20);
                          private var _myURL:String = new String("url");

                   

                          public function Images(path:String, myWidth:uint, myHeight:uint, myX:uint, myY:uint, myURL:String="")
                          {
                              _path = path;
                              _myURL = myURL;
                              _myWidth = myWidth;
                              _myHeight = myHeight;
                              _myX = myX;
                              _myY = myY;
                              LoadImage();
                          }
                         
                          function LoadImage():void
                          {
                              parent_container = new Sprite();
                              addChild(parent_container)
                              var req:URLRequest = new URLRequest(_path);
                              var loader:Loader = new Loader();
                              loader.load(req);
                              loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);         
                              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadedImage);
                          }

                   

                          function LoadedImage(e:Event):void
                          {
                              var loaderInfo:LoaderInfo = e.target as LoaderInfo;
                              var displayObject:DisplayObject = loaderInfo.content;
                              displayObject.width = _myWidth;
                              displayObject.height = _myHeight;
                              parent_container.addChild(displayObject);
                              parent_container.x = _myX;
                              parent_container.y = _myY;

                  dispatchEvent(new Event("loadcomplete"));
                          }
                         
                          function ioErrorHandler(event:IOErrorEvent):void {
                              trace("ioErrorHandler: " + event);
                          }
                      }
                  }


                  • 6. Re: Loader Q
                    beno12345 Level 1

                    Ah, so I put in a couple traces, one after the addEventListener and another after the DispatchEvent, expecting them to neatly alternate one after the other. But no, I'd get one of the former and then many traces of the latter before getting another of the former: 17, then 3, then 8, then 9...no rhyme nor reason that I could see to it. Of course the images didn't come up like they were supposed to, either. Wierd. Any suggestions?

                    TIA,

                    Beno

                    • 7. Re: Loader Q
                      kglad Adobe Community Professional & MVP

                      copy and paste the code you used (with the trace() statements) and copy and paste the first 20 or so trace output lines.

                      • 8. Re: Loader Q
                        beno12345 Level 1

                                private function BackgroundImages(e:Event):void
                                {
                                    var img_container:Sprite = new Sprite();
                                    if(index<indexLimit)
                                    {
                                        var img:Images = new Images(bkgnd_imgs[index], 1006,523, 0, 0);
                                        img.addEventListener("loadcomplete",BackgroundImages);
                                        trace("two");
                                        img.x = 1006 * index;
                                        img_container.addChild(img);
                                        index++;
                                    }
                                    addChild(img_container);
                                    Blackout();
                                    var timeline:TimelineMax = new TimelineMax({repeat:-1});
                                    while (tweenCount++ < 7)
                                    {
                                        timeline.append(TweenLite.to(img_container, 1, {x:-1006*tweenCount, delay:5, onStart:getTweenCount, onStartParams:[tweenCount]}));
                                    }
                                    timeline.play();
                                    BuildMainTextSprite();
                                }

                         

                         

                        [[from Images]]

                         

                                function LoadedImage(e:Event):void
                                {
                                    var loaderInfo:LoaderInfo = e.target as LoaderInfo;
                                    var displayObject:DisplayObject = loaderInfo.content;
                                    displayObject.width = _myWidth;
                                    displayObject.height = _myHeight;
                                    parent_container.addChild(displayObject);
                                    parent_container.x = _myX;
                                    parent_container.y = _myY;
                                    dispatchEvent(new Event("loadcomplete"));
                                    trace("one");
                                }

                         

                        [[traces]]

                         

                        two
                        one
                        one
                        one
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        one
                        one
                        ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
                            at flash.display::DisplayObjectContainer/removeChild()
                            at Function/<anonymous>()
                            at Function/http://adobe.com/AS3/2006/builtin::apply()
                            at com.greensock.core::TweenCore/complete()
                            at com.greensock::TimelineLite/renderTime()
                            at com.greensock.core::SimpleTimeline/renderTime()
                            at com.greensock::TweenLite$/updateAll()
                        one
                        one
                        one
                        one
                        one
                        one
                        two
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one
                        one

                         

                        TIA,

                        Bno

                        • 9. Re: Loader Q
                          kglad Adobe Community Professional & MVP

                          i just copied the code from my message 5 and it works as expected.  i added a trace(index) in the first part of the code and i added a trace("dispatch") in the class file.  here's the output:

                           

                          0
                          dispatch
                          1
                          dispatch
                          2
                          dispatch
                          3
                          dispatch
                          4
                          dispatch
                          5
                          dispatch
                          6
                          dispatch

                           

                           

                          so, you have code elsewhere that's causing the problem.

                          • 10. Re: Loader Q
                            beno12345 Level 1

                            Er, well, that's 'cause I call "Images" a gazillion times outside of the need for the dispatch event. Oops. Sorry. Fixed. Thanks.

                            Beno

                            • 11. Re: Loader Q
                              kglad Adobe Community Professional & MVP

                              you're welcome.