5 Replies Latest reply on Dec 24, 2010 12:58 PM by Andrei1

    Loading an Image With AS3

    beno12345 Level 1

      Hi;

      I have this code:

       

              function LoadLogo():void
              {
                  parent_container = new Sprite();
                  addChild(parent_container)
                  var path:String = "images/logo.png";
                  var req:URLRequest = new URLRequest(path);
                  var loader:Loader = new Loader();
                  loader.load(req);
                  loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ trace(e) });         
                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadedLogo);
                  trace('xxx');
              }

       

              function LoadedLogo(evt:Event):void
              {
                  var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
                  var displayObject:DisplayObject = loaderInfo.content;
                  displayObject.width = 175;
                  displayObject.height = 175;
                  parent_container.addChild(displayObject);
                  parent_container.x = 20;
                  parent_container.y = 20;
              }

       

      Works fine in another file, but not here. The trace prints. The error never does...even when the path is deliberately incorrect. The trace doesn't print if it's in the first line of LoadedLogo. No error is ever thrown. Huh? Please help.

      TIA,

      beno

        • 1. Re: Loading an Image With AS3
          Andrei1 Level 6

          This is an extremely bad practice to use anonymous functions. I suggest you move error handler into a separate function.

          • 2. Re: Loading an Image With AS3
            beno12345 Level 1

            Why is it bad practice? Did it, made no difference. I hope someone will answer my original question.

            beno

            • 3. Re: Loading an Image With AS3
              Andrei1 Level 6

              For one you cannot efficiently make your object available for garbage collection. There are many other reasons too.

              .

              As for your original question - error event listener has to be added to contentLoaderInfo as well.

              • 4. Re: Loading an Image With AS3
                beno12345 Level 1

                OK. Revised code:

                 

                        function LoadLogo():void
                        {
                            parent_container = new Sprite();
                            addChild(parent_container)
                            var path:String = "images/logo.png";
                            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, LoadedLogo);
                            trace('xxx');
                        }

                 

                        function LoadedLogo(evt:Event):void
                        {
                            var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
                            var displayObject:DisplayObject = loaderInfo.content;
                            displayObject.width = 150;
                            displayObject.height = 150;
                            parent_container.addChild(displayObject);
                            parent_container.x = 20;
                            parent_container.y = 20;
                            /*
                            Navigation(' Home ','index.html',269,0xffffff);
                            Navigation(' Why Us? ','Why.html',330,0xffffff);
                            Navigation(' Our Team ','Our_Team.html',410,0xffffff);
                            Navigation(' Government Projects ','Government.html',497,0xffffff);
                            Navigation(' About Us ','About_Us.html',656,0xffffff);
                            Navigation(' Contact Us ','Contact_Us.html',735,0xffffff);
                            XMLLoader();
                            */
                        }
                       
                        function ioErrorHandler(event:IOErrorEvent):void {
                            trace("ioErrorHandler: " + event);
                       }

                Please advise how to add the ErrorEventListener to the contentLoaderInfo (maybe an example?).

                TIA,

                beno

                • 5. Re: Loading an Image With AS3
                  Andrei1 Level 6

                  Again, you need to add error listener to loader.contentLoaderInfo - not loader

                   

                  loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);