1

我创建了一个容器(或影片剪辑),它将按顺序添加照片并显示在舞台上。但是,由于文件大小不同,它会先添加最小文件大小的照片。我该如何解决这个问题。请看下面的示例代码

// image0.jpg -> 3k
// image1.jpg -> 2k
// image2.jpg -> 1k
// image3.jpg -> 2k
// image5.jpg -> 1k

var photoPath:Array = new Array();
var photoContainer:MovieClip = new MovieClip();

photoPath.push('image0.jpg');
photoPath.push('image1.jpg');
photoPath.push('image2.jpg');
photoPath.push('image3.jpg');
photoPath.push('image4.jpg');

for(var i = 0; i < photoPath.length; i++)
{
    var loader:Loader = new Loader();
    loader.load(URLRequest(photoPath[i]));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded);
}

function photoLoaded(e:Event):void
{
    photoContainer.addChild(e.target.content);
}

//output will looks like (image2,image5,image1,image3,image0) instead of 0,1,2,3,4,5
4

2 回答 2

1

有很多方法可以处理这个问题,可能最简单的改造当前代码的方法是在实例化时将加载器添加到显示列表中。这将保证它们以正确的顺序放置在 photoContainer 中。如果您绝对需要位图本身而不是加载器本身在显示列表中,您可以在您的 photoLoaded 方法中解决这个问题:

import flash.display.Loader;

var photoPath:Array = new Array();
var photoContainer:MovieClip = new MovieClip();

photoPath.push('image0.jpg');
photoPath.push('image1.jpg');
photoPath.push('image2.jpg');
photoPath.push('image3.jpg');
photoPath.push('image4.jpg');

for(var i = 0; i < photoPath.length; i++)
{
    var loader:Loader = new Loader();
    photoContainer.addChild(loader);  //add the loader to photoContainer
    loader.load(new URLRequest(photoPath[i]));
    //you only actually have to listen for complete below if you want to replace the loader with its contents 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded);
}

function photoLoaded(e:Event):void
{
    var loader:Loader = e.target.loader;
    var index:int = photoContainer.getChildIndex(loader);
    // this will add the contents of the loader in the same order the loader was originally added
    photoContainer.addChildAt(loader.content, index);
    photoContainer.removeChild(loader);
}
于 2013-02-08T20:40:56.893 回答
0

只需按照数组中给定的顺序加载照片并将其添加到容器中:

var photoPath:Array = new Array();
var photoContainer:MovieClip = new MovieClip();
var i:int = 0;

photoPath.push('image0.jpg');
photoPath.push('image1.jpg');
photoPath.push('image2.jpg');
photoPath.push('image3.jpg');
photoPath.push('image4.jpg');

loadPhoto(i);

function loadPhoto(i:int):void
{
    var loader:Loader = new Loader();
    loader.load(new URLRequest(photoPath[i]));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded);
}

function photoLoaded(e:Event):void
{
    photoContainer.addChild(e.target.content);
    trace(e.target.url); // Verify
    if(i<photoPath.length-1) loadPhoto(++i);
}

输出:

image0.jpg
image1.jpg
image2.jpg
image3.jpg
image4.jpg
于 2013-02-08T20:48:31.007 回答