I am going to show off my newbishness with the following question :)

I have a simple application called URLLoaderTest which has an initApp() function and a couple of controls. In the initApp function I initialise a class which contains a URLLoader (slightly modified version from the livedocs). That class called MyURLLoader loads a jpeg from a URL. I then try to reference the returned jpeg to display through an image control. The jpeg  is retrieved successfully by the loader but the event is handled after I've tried to assign it back to the image control in the application. The data is there but the execution order is off.

Stepping through the debugger the entire initApp() function executes before the events from MyURLLoader are handled. How can I get MyURLLoader to handle events immediately and then return to initApp() to complete?

I've included the code below as my explanation is probably confusing. I just don't think I understand the event flow. Am I missing a listener in application?

Many thanks
Angus

PS. I initially thought it was due to scoping so the data is pushed into a public variable and I also coded a getter which you will see below.

// URLLoaderTest.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" creationComplete="initApp()">
<mx:Script>
    <![CDATA[
        import MyURLLoader;
        import flash.util.trace;
        import mx.controls.Image;
               
        private function initApp() {
            var JPGLoader:MyURLLoader = new MyURLLoader();
            var myImage:Image = new mx.controls.Image();
            myImage.dataObject = JPGLoader.getData();
            myBox.addChild(myImage);
        }
    ]]>
</mx:Script>   
    <mx:Canvas width="100%" height="100%">
        <mx:HBox id="myBox">
        </mx:HBox>
    </mx:Canvas>
</mx:Application>

// MyURLLoader.as

package {
    import flash.util.trace;
    import flash.display.Sprite;
    import flash.net.*;
    import flash.events.*;
   
    public class MyURLLoader extends Sprite {
       
        // public properties
        public var myJPG;
       
        public function MyURLLoader() {
            var loader:URLLoader  = new URLLoader();
            configureListeners(loader);
            var request:URLRequest = new URLRequest("http://flextest/sample.jpg");
            loader.load(request);
        }
        public function getData() {
            trace("getData");
            return this.myJPG;
        }
        private function configureListeners(dispatcher:IEventDispatcher):Void {
            trace("configure listeners");
            dispatcher.addEventListener(EventType.COMPLETE, onComplete);
            dispatcher.addEventListener(EventType.OPEN, onOpen);
            dispatcher.addEventListener(ProgressEventType.PROGRESS, onProgress);
            dispatcher.addEventListener(SecurityErrorEventType.SECURITY_ERROR, onSecurityError);
            dispatcher.addEventListener(HTTPStatusEventType.HTTP_STATUS, onHTTPStatus);
            dispatcher.addEventListener(IOErrorEventType.IO_ERROR, onIOError);
        }
        private function onComplete(event:Event):Void {
            var loader:URLLoader = URLLoader(event.target);
            this.myJPG = loader.data;
            trace("onLoaded: " + loader.data);
        }
        private function onHTTPStatus(event:HTTPStatusEvent):Void {
            trace("onHTTPStatus: " + event);
        }
        private function onIOError(event:IOErrorEvent):Void {
            trace("onIOError: " + event);
        }
        private function onOpen(event:Event):Void {
            trace("onOpen: " + event);
        }
        private function onProgress(event:ProgressEvent):Void {
            trace("onProgress loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }
        private function onSecurityError(event:SecurityErrorEvent):Void {
            trace("onSecurityError: " + event);
        }
    }
}


--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




SPONSORED LINKS
Web site design development Computer software development Software design and development
Macromedia flex Software development best practice


YAHOO! GROUPS LINKS




Reply via email to