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

发布于 2024-07-10 14:01:42 字数 486 浏览 6 评论 0原文

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

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

Im currently using the following function to load an image, however i could not figure out a way to find the width of the loaded image, which i intend to use before placing the next image using the same function.

Note that q is a a variable (a number) which is used to load differant images.

=X i need help obtainning the loaded image width...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狠疯拽 2024-07-17 14:01:42

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

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

或者 此链接可能有帮助吗? 自己没试过...

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

焚却相思 2024-07-17 14:01:42

我只是询问加载器对象的 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 是画布尺寸......

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

放赐 2024-07-17 14:01:42

要访问宽度,您必须在分配给处理 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.
        }
    }
}

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

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.

南巷近海 2024-07-17 14:01:42

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文