5

我目前使用以下函数来加载图像,但是我无法找到一种方法来找到加载图像的宽度,我打算在使用相同的函数放置下一个图像之前使用它。

请注意,q 是一个变量(一个数字),用于加载不同的图像。

=X 我需要帮助来获取加载的图像宽度...

function LoadImage(q)
{
    var imageLoader:Loader = new Loader();
    var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
    imageLoader.load(image);
    addChild (imageLoader);
    imageLoader.x = 0 + CurrentXLength;
    imageLoader.y = 0;
    imageLoader.name = "image"+q;
    trace(imageLoader.x)
}
4

4 回答 4

12

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 ...

于 2008-12-23T16:24:07.260 回答
2

我只是要求加载器对象的宽度属性:

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);
}

其中 1360 和 768 是画布尺寸...

于 2011-12-20T10:02:27.290 回答
1

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.

于 2008-12-23T16:00:14.657 回答
0

是的,我使用了 scott 的答案,但值得注意的是 LoadImage() 函数中的 'imageLoader.contentLoader' 应该是 'imageLoader.contentLoaderInfo'。为我解决了宽度问题——感谢 mang

于 2009-08-28T02:57:13.963 回答