如何“在UILoader中即时显示图像。”?

时间:2011-12-05 15:14:21

标签: flash actionscript-3

大家好我想点击按钮时切换图像。所以我有这个代码。

var k2:Boolean = false;
btn1.addEventListener(MouseEvent.CLICK, imgChanger);
function imgChanger(e:Event):void
{
    if (k2==false)
    {
        img1.myUILoader.source = "img/01.jpg";
        k2 = true;
    }
    else
    {
        img2.myUILoader.source = "img/02.jpg";
    }
}

但是对于大图像来说并不是很好解决,因为每次单击按钮都会下载它们。我想在网站加载时下载此图片。

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

继承人你可以使用的......这会在后台加载所有图像,并在第一个加载时给你通知,这样你就可以立即显示它并加载所有图像...如果有很多图像我建议只加载下一个...并非所有这些都不是记忆猪..

var loadedImages:Vector.<DisplayObject> = new Vector.<DisplayObject>;
var imageUrls:Vector.<String> = new Vector.<String>;

// set your image urls, manually, from xml... whatever
imageUrls.push("http://www.mySite.com/image0.jpg");
imageUrls.push("http://www.mySite.com/image1.jpg");
imageUrls.push("http://www.mySite.com/image2.jpg");

// load all image sequentially
var curImageLoadingIndex:int=0;
loadNextImage();
/** loads each image sequentially via recursive calls */
function loadNextImage():void{
    if (curImageLoadingIndex>=imageUrls.length){// done loading
        allImagesLoaded()
    }else{// load next one
        var ldr:Loader=new Loader();
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
                    ldr.load(new URLRequest(imageUrls[curImageLoadingIndex]));

        function loaded(event:Event):void{
            ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaded);//remove listener to prevent memory leaks
            loadedImages.push(ldr);//store loaded image for user whenever needed
            if (curImageLoadingIndex==0)
                firstImageReady();
            curImageLoadingIndex++;//increment to load the next item
            loadNextImage();
        }
    }
}

/** notificaltion when all the images have been loaded */
function allImagesLoaded():void{
    // know all images are loaded here... 
}

/** first image ready to use */
function firstImageReady():void{
    var fistImage:DisplayObject = loadedImages[0];
    // do something with your image here... 
}

如果您想使用UILoader,只需在请求中更改2行

    var ldr:Loader=new UILoader();
    ldr.addEventListener(Event.COMPLETE,loaded);