在AS3中查找(加载)图像大小(Action Script 3.0)

5 size image actionscript-3 dimension

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

请注意,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)
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*den 12

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

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;
    ....
Run Code Online (Sandbox Code Playgroud)

或者这个链接可能会有帮助吗?我自己没试过......


Bri*_*dge 1

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

“您将需要一个包含您希望加载的项目的数组。您可能应该使用 XML 加载此数据,以便它是动态且可扩展的。加载 xml 数据后,应该以您喜欢的任何方式将其分配给数组。在这种情况下,我们必须使用数组,而不仅仅是使用 XML 对象(它本质上是一个数组),是因为您需要知道对象宽度,以便可以将下一个对象 X 位置置于最后一个对象 X 的基础上位置加上其宽度。

对于 XML,通常使用 for 循环并仅迭代“x”次。在这种情况下,我们不希望出现这种情况。要获取已加载资源的“WIDTH”属性,必须在加载程序触发 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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助,代码未经测试,但这个概念似乎是有效的。