Finding (loaded) image size in AS3 (Action Script 3.0)

江枫思渺然 提交于 2019-11-30 15:12:05

You can't know the width of the bitmap until it's actually loaded:

function LoadImage(q)
{
    var imageLoader:Loader = new Loader();
    var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
    imageLoader.contentLoader.addEventListener(Event.COMPLETE, ImageLoaded);
    imageLoader.load(image);
    addChild (imageLoader);
    ...


private function ImageLoaded(e:Event):void
{
    var imageLoader:Loader = Loader(e.target.loader);
    var bm:Bitmap = Bitmap(imageLoader.content);
    var CurrentXLength = bm.width;
    ....

Alternativly this link might be helpful? Haven't tried it myself ...

I just asked for de width property of loader object:

var loader:Loader;

function loadImage(dir:String):void {
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, placeImage);
    var urlReq:URLRequest = new URLRequest(direccion);
    loader.load(urlReq);
}

function placeImage(o:Event):void {
    loader.x = (1360 - loader.width)/2;
    loader.y = (768 - loader.height)/2;
    addChild(loader);
}

Where 1360 and 768 are the canvas dimensions...

Brian Hodge

To access the width you must do it within the function assigned to handle Event.COMPLETE.

"You will want an array containing the items you wish to load. You should probably load this data in with XML so it is dynamic and scalable. Once the xml data is loaded it should be assigned to an array in whatever fashion you like. The reason that we must use an array in this situation, rather then just using the XML object, which is essentially an array, is because you need the know an objects width so that you can base the next objects X position off of the last objects X position plus its WIDTH.

With XML it is common to use a for loop and just iterate through "x" amount of times. We do not want this, in this case. To obtain the "WIDTH" property of the loaded asset, it must be accessed from within the function assigned to fire when the loader fires Event.COMPLETE. Once the image has completed it will remove the item from the array, set a variable as to the lastX and lastWidth, and then get the next item in the array and start all over. Eventually the array is empty and the process is complete.

-Note: I will skip loading the XML and just inject the data into the array myself.

package
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.net.URLRequest;

    public class DocumentClass extends Sprite
    {
        private var _array:Array;
        private var _lastX:Number;
        private var _lastWidth:Number;

        public function DocumentClass():void
        {
            _array = new Array();
            //Add Items to an array however you wish.  I did it
            //this way to make it easier to read on this site.

            _array.push({name: "image1", path: "path/to/image1"});
            _array.push({name: "image2", path: "path/to/image2"});
            _array.push({name: "image3", path: "path/to/image3"});

            loadImage();
        }
        private function loadImage():void
        {
            if(_array.length > 0)
            {
                var loader:Loader = new Loader();
                addChild(loader);

                var request:URLRequest = new URLRequest(_array[0].path); //Loads first item in the array each time.
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
                loader.x = _lastX + _lastWidth;
                laoder.load(request);

                _lastX = loader.x; //We set after so we are ready for the next.

                _array.shift(); //Remove the first item from the array.
            }
        }
        function onImageLoaded(e:Event):void
        {
            _lastWidth = e.target.width;
            loadImage(); //Attempt to load another image if the array isn't empty.
        }
    }
}

I hope this helps, the code isn't tested, but the concept seems valid.

Yeah I used scott's answer but it's worth noting that 'imageLoader.contentLoader' should be 'imageLoader.contentLoaderInfo' in the LoadImage() function. Solved the width prob for me-- thanks mang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!