Hi,

data loading in Flash Player (and therefore in your Flex application)
happens asynchronously. You'll have to wait until the JPG is loaded
completely before you can access the data. 

The best way to handle this would be to have your MyURLLoader class
dispatch an Event (e.g. of type "dataLoaded") after the image is loaded
(the below is untested email code - but it should point you to the right
directrion):

// inside MyURLLoader 
private function onComplete(event:Event):Void {
  var loader:URLLoader = URLLoader(event.target);
  this.myJPG = loader.data;
  var e:Event = new Event();
  e.type = "dataLoaded";
  dispatchEvent(e);
}

Of course, you'll now need to add a listener to the MyURLLoader instance
itself to get informed when the imgae is downloaded completely:

// URLLoaderTest.mxml
private function initApp() {
  var JPGLoader:MyURLLoader = new MyURLLoader();
  JPGLoader.addEventListener("dataLoaded", imageLoaded);
  var myImage:Image = new mx.controls.Image();
  myBox.addChild(myImage);
}

private function imageLoaded(event:Event):Void {
  myImage.dataObject = MyURLLoader(event.target.getData());
}


Dirk.


________________________________

        From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Angus Johnson
        Sent: Tuesday, January 17, 2006 10:50 AM
        To: [email protected]
        Subject: [flexcoders] event flow / URLLoader
        
        
        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
<http://groups.yahoo.com/gads?t=ms&k=Web+site+design+development&w1=Web+
site+design+development&w2=Computer+software+development&w3=Software+des
ign+and+development&w4=Macromedia+flex&w5=Software+development+best+prac
tice&c=5&s=166&.sig=L-4QTvxB_quFDtMyhrQaHQ>     Computer software
development
<http://groups.yahoo.com/gads?t=ms&k=Computer+software+development&w1=We
b+site+design+development&w2=Computer+software+development&w3=Software+d
esign+and+development&w4=Macromedia+flex&w5=Software+development+best+pr
actice&c=5&s=166&.sig=lvQjSRfQDfWudJSe1lLjHw>   Software design and
development
<http://groups.yahoo.com/gads?t=ms&k=Software+design+and+development&w1=
Web+site+design+development&w2=Computer+software+development&w3=Software
+design+and+development&w4=Macromedia+flex&w5=Software+development+best+
practice&c=5&s=166&.sig=1pMBCdo3DsJbuU9AEmO1oQ>         
        Macromedia flex
<http://groups.yahoo.com/gads?t=ms&k=Macromedia+flex&w1=Web+site+design+
development&w2=Computer+software+development&w3=Software+design+and+deve
lopment&w4=Macromedia+flex&w5=Software+development+best+practice&c=5&s=1
66&.sig=OO6nPIrz7_EpZI36cYzBjw>         Software development best
practice
<http://groups.yahoo.com/gads?t=ms&k=Software+development+best+practice&;
w1=Web+site+design+development&w2=Computer+software+development&w3=Softw
are+design+and+development&w4=Macromedia+flex&w5=Software+development+be
st+practice&c=5&s=166&.sig=f89quyyulIDsnABLD6IXIw>      

________________________________

        YAHOO! GROUPS LINKS 


                        *        Visit your group "flexcoders
<http://groups.yahoo.com/group/flexcoders> " on the web.
                  
        *        To unsubscribe from this group, send an email to:
                 [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> 
                  
        *        Your use of Yahoo! Groups is subject to the Yahoo!
Terms of Service <http://docs.yahoo.com/info/terms/> . 


________________________________




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

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to