views:

224

answers:

3
package
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLRequest;

    public class Main extends MovieClip
    {
        // state
        private var iname:String = "image.gif";
        private var w:int;
        private var h:int;
        private var loader:Loader;

        // constructor
        public function Main():void
        {
            loadImage( iname ); 
            trace( w + " " + h );

        }

        // methods
        private function loadImage( filename:String ):void
        {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener( Event.COMPLETE, completeHandler );
            loader.contentLoaderInfo.addEventListener( Event.COMPLETE, someHandler );
            var request:URLRequest = new URLRequest( filename );
            loader.load( request );
            this.addChild( loader );        
        }



        // event handlers
        private function completeHandler( event:Event ):void
        {           
            trace( "image loaded\n" + event.target.url );
            trace( event.target );
        }           

        private function someHandler( event:Event ):void
        {
            w = loader.contentLoaderInfo.width;
            h = loader.contentLoaderInfo.height;            
        }
    }
}

But when i ran i get 0 0 for width and height. How can i get the correct values ? Also if the picture is loaded correctly and after i attach it to a movieclip why i cannot take the width and height from movieclip object?

A: 

The loading of your image is not instantaneous.
Trace the width and height in one of your Event.COMPLETE handlers and it will work just fine!

grapefrukt
yes as i said to Omnomlets both you are correct. But my problem is to make known those values to the constructor body and use them as arguments to instantiate a new object from the class's constructor (or in another way if possible).
Ponty
+1  A: 

Is it just me or are you asking for the width and height before the Complete event fires? What results do you get if you move your w and h trace into the "someHandler" function, after you set them?

Austin Fitzpatrick
yes you are right, if i move the trace into the "someHandler" function then i take the dimensions correctly. My problem though is that i want to use those dimension in the constructor to make a BitmapData object from the Loader content and i need the width and height to instantiate one. How can i do that since the values seem unknown on the constructor body ?
Ponty
This won't be possible, you'll need to create the bitmap data after the loader has finished the load and then maybe dispatch an event so that whatever needs that bitmap data knows that it is ready, and then uses it. If you're preloading everything you can have all of this done while the loading bar is showing so that when your application actually launches the information is ready and you can do whatever you want with it.
Austin Fitzpatrick
A: 

You're asking for the contentLoaderInfo.height... you should be asking for the content height.

h = event.target.content.height;
w = event.target.content.width;
Jascha
You can get the width and height of contentLoaderInfo and it will return the nominal width or height of the object regardless of the scaling value.
wmid
nice tip wpjmurray
Ponty