开发者

How to get width and height from Loader object

开发者 https://www.devze.com 2022-12-21 01:58 出处:网络
package { import flash.display.*; import flash.events.*; import flash.net.URLRequest; public class Main extends MovieClip
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( reque开发者_JAVA技巧st );
            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?


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?


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!


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消