4 Replies Latest reply on May 3, 2009 8:45 AM by Gregory Lafrance

    how to run code after each other?

    Starlover_jacob Level 1

      Hi there, a little question.

       

      I call a function that returns the path of a new saved image as a string.

      In that function I  use a loader to load the image. After the image is loaded I set the url of the saved image and return it.

       

      Next = id of the new saved image

      Imageurl = the url of the image to load.

      thumborfull = an indication to see if the thumb needs to be saved or the full image, so the thumb can be saved in a different folder than the full image.

       

      My problem: when I call this function.. the return goes first, and then the loader complete. In the loader complete I set the new path of the image which i want to return.

      So, my code runs synchronal, and not synchronic.. How do you get it to run synchronic?

       

      Please some help.

       

      code:

           public var myimageurl:String;
           public function saveImageFromInternet(next:Number, imageurl:String, thumborfull:String):String
           {
                var imgClass:Class;
                //the second way
                var loader:Loader;
        
                //source code of the second way
                loader = new Loader();
                //notice: NOT _loader.addEventListener,is  _loader.contentLoaderInfo.addEventListener
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event):void
                { 
                      var loader:Loader = Loader(event.target.loader);
                          // width and heigt of selected image
                     var originalimgwidth:Number=event.target.width;
                     var originalimgheight:Number=event.target.height;
                     
                     //put original image into bitmapdata
                     var bitmapje:BitmapData;
                     bitmapje=new BitmapData(originalimgwidth, originalimgheight, false, 0xFFFFFFFF );
                     bitmapje.draw(loader.content,null,null,null,null,true);          
                     
                     var newimagefile:File=new File();
                     if(thumborfull=="thumb")
                     {
                          newimagefile=docsDir.resolvePath("Tmtp/I"+next+".mtp"); //image path
                          myimageurl=newimagefile.nativePath;
                     }
                     else
                     {
                          newimagefile=docsDir.resolvePath("Fmtp/I"+next+".mtp"); //image path
                          myimageurl=newimagefile.nativePath;               
                     }          
                     var stream:FileStream = new FileStream;          // create new filestream
                     stream.open(newimagefile, FileMode.WRITE);     // open filestream
                     var data:ByteArray = encodeToJpg(bitmapje);     // convert bitmapdata to a jpg bytearry
                     stream.writeBytes(data, 0, data.length);     // writing the image
                     stream.close();
                });
                loader.load(new URLRequest(encodeURI(imageurl)));
                return myimageurl;
           }