Kopy_Kat
Kopy_Kat

Reputation: 25

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions. question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:

 var loader:Loader=new Loader();

 var urlR:URLRequest=new URLRequest("Image.jpg");   
 public function main()   
{  
 loader.load(urlR);  
 var h:Number = loader.height;// here instead of the width of the image of h is set to 0   
 // And if you do like this:    
 DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below   
 // 256 is displayed, as necessary  
}
  private function DrawText(text:String, x:int, y:int):void  
{  
          var txt:TextField = new TextField();  
 txt.autoSize = TextFieldAutoSize.LEFT;  
 txt.background = true;  
 txt.border = true;  
 txt.backgroundColor = 0xff000000;

 var tFor:TextFormat = new TextFormat();   
 tFor.font = "Charlemagne Std";   
 tFor.color = 0xff00ff00;   
 tFor.size = 20;  

 txt.x = x;   
 txt.y = y;   
 txt.text = text;   
 txt.setTextFormat(tFor);   

addChild(txt);  
  }      

Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.

Upvotes: 1

Views: 66

Answers (2)

putvande
putvande

Reputation: 15213

You need to wait for your image to load to be able to get values out of it. Attach an eventListener to your URLLoader.

var urlR:URLRequest = new URLRequest("Image.jpg");   
loader.load(urlR);  
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);

function loader_complete(e:Event): void {
    // Here you can get the height and width values etc.
    var target_mc:Loader = evt.currentTarget.loader as Loader;
    // target_mc.height , target_mc.width
}

Loader

Upvotes: 2

KRog
KRog

Reputation: 121

Well it's simple.
Flash is focused on the Internet, hence such problems.

If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach. Yeah plus still highly dependent on the size of the file that you read. Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.

Upvotes: 2

Related Questions