Actionscript 3 在AS3中查找(加载)图像大小(操作脚本3.0)

Actionscript 3 在AS3中查找(加载)图像大小(操作脚本3.0),actionscript-3,image,size,dimension,Actionscript 3,Image,Size,Dimension,我目前正在使用以下函数加载图像,但是我想不出一种方法来找到加载图像的宽度,我打算在使用相同的函数放置下一幅图像之前使用该宽度 请注意,q是一个变量(一个数字),用于加载不同的图像 =X我需要帮助获取加载的图像宽度 function LoadImage(q) { var imageLoader:Loader = new Loader(); var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");

我目前正在使用以下函数加载图像,但是我想不出一种方法来找到加载图像的宽度,我打算在使用相同的函数放置下一幅图像之前使用该宽度

请注意,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)
}

要访问宽度,必须在分配给处理Event.COMPLETE的函数中执行

“您需要一个包含要加载的项的数组。您可能应该使用XML加载此数据,使其具有动态性和可伸缩性。加载xml数据后,应以您喜欢的任何方式将其分配给数组。在这种情况下,我们必须使用数组,而不仅仅是使用XML对象,它本质上是一个数组,因为您需要知道对象的宽度,以便可以将下一个对象的X位置从上一个对象的X位置加上它的宽度

对于XML,通常使用for循环,只需在“x”中迭代多次。在这种情况下,我们不希望这样。要获取加载的资源的“宽度”属性,必须在加载程序激发Event.COMPLETE时从分配给激发的函数中访问该属性。一旦图像完成,它将从数组中删除该项,设置一个变量作为lastX和lastWidth,然后获取数组中的下一项并重新开始。最后,数组是空的,过程就完成了

-注意:我将跳过加载XML,自己将数据注入数组

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

我希望这会有所帮助,代码没有经过测试,但概念似乎有效。

在实际加载位图之前,您无法知道位图的宽度:

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

或者可能有帮助?我自己还没有试过…

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

我刚刚询问了loader对象的de width属性:

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是画布维度…

谢谢,这很有帮助,尽管我认为代码是
content\u loader.contentLoaderInfo.addEventListener(Event.CO
而不是
imageLoader.contentLoader
。。。