繁体   English   中英

Flash AS3:根据图像高度从循环定位加载的图像

[英]Flash AS3: position loaded images from loop based on image height

我正在尝试动态堆叠通过xml文件插入的图像。 下面是我在做什么,并且几乎可以正常工作。 问题在于,它似乎仅在最后一个函数上触发了事件完成功能,而不是全部执行。 有没有办法让它为每个图像运行even.complete函数?

function aboutfileLoaded(event:Event):void {
    aboutXML = new XML(aboutTextLoader.data);
for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++)
    {
            imageLoader = new Loader();
            imageSource = aboutXML.aboutimages.image[l];

            if (imageSource != "") {
                    this.imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
                    this.imageLoader.load(new URLRequest(imageSource));

                    //aboutBox.aboutContent.addChild(imageLoader);
                    //imageLoader.y = imageYpos;
                    //imageYpos = imageYpos + 50;
            }

    }
}

function aboutimageLoaded(event:Event):void {
            aboutBox.aboutContent.addChild(imageLoader);
            this.imageLoader.y = imageYpos;
            imageYpos = imageYpos + this.imageLoader.height;
}

我使用编写的简单Image类加载所有图像。

public function loadImages(xml:XML):void {
    for each(var img:XML in xml.images) {
        var i:Image = new Image(img.src, img.width, img.height);

        i.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, positionImage);
    }
}

/**/

private function positionImage(e:Event):void {
    var i:Image = e.target;
    gallery.addChild(i);
    // Do the positioning.
}

在上面的代码中,您始终可以只更改aboutimageLoaded函数:

// In aboutfileLoaded() change all "this.imageLoader" to just "imageLoader"

function aboutimageLoader(event:Event):void {
    aboutBox.aboutContent.addChild(event.target);
    event.target.y = imageYpos;
}

每个加载程序一次只能处理一个作业,因此您要么要堆叠作业以一个接一个地加载:

//Global vars
loadIndex:int = 0; 
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
loadImage();

private function loadimage(){
    if(aboutXML.aboutimages.image[loadIndex] != null){
        imageSource = aboutXML.aboutimages.image[loadIndex];
        if (imageSource != "") {
            imageLoader.load(new URLRequest(imageSource));
        }
    }
}

function aboutimageLoaded(event:Event):void {
    var holder = (ev.content as Bitmap).clone();
    aboutBox.aboutContent.addChild(holder);
    holder .y = imageYpos;
    imageYpos = imageYpos + holder.height;
    loadIndex ++;
    loadimage();
}

或制作多个Loader实例,每个实例一个:

for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++)
    {
            var imageLoaderTemp = new Loader();
            imageSource = aboutXML.aboutimages.image[l];

            if (imageSource != "") {
                    imageLoaderTemp.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
                    imageLoaderTemp.load(new URLRequest(imageSource));
            }
    }
}

function aboutimageLoaded(event:Event):void {
            aboutBox.aboutContent.addChild(event.target);
            event.target.y = imageYpos;
            imageYpos = imageYpos + event.target.height;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM