1

为清楚起见进行了编辑:我想在 for 循环中从外部 url 加载各种图像。我想循环调用它们,当它结束时,我再次开始循环。但是我不想用“http请求”再次调用它们,而是我想一遍又一遍地循环加载图像。

是否可以在循环中创建动态加载器名称?

这是代码示例:

var Count = 0;
// Loop Begins
var Count:Loader = new Loader();
Count.load(new URLRequest("myurl");
addChild(Count);
Count++;
// End Loop

另一个例子是有一个名字,然后在最后加上数字。但是如何

var Count = 0;
// Loop Begins
var MyName+Count:Loader = new Loader(); 
MyName+Count.load(new URLRequest("myurl");
addChild(MyName+Count);
Count++;
// End Loop

简而言之:我想将一堆图像加载到一个数组中,并通过稍后调用它们来循环加载的图像。

非常感谢!

4

2 回答 2

2

CASE1 代码是如何在 Sequence 中加载图像。

CASE2 代码是如何在 Synchronized 中加载图像。

首先,您要导入所有图像的 URL 必须按顺序命名。例如 必须采用以下格式:

www.myURL.com/img0.jpg 
www.myURL.com/img1.jpg 
www.myURL.com/img2.jpg 
www.myURL.com/img3.jpg 
www.myURL.com/img4.jpg 
www.myURL.com/img5.jpg
        .
        .
        .

试试下面的代码,只是一个测试。

CASE1

var   imgLoader:Loader;
var   imgRequest:URLRequest;
var   count:int = -1;
const TOTAL_COUNT:int = 10;
var   imgBox:Array = [];
var   isCounting:Boolean;

function loadImage():void
{
    count ++;

    isCounting = true;

    imgLoader = new Loader();
    imgRequest = new URLRequest();
    imgRequest.url = "www.myURL.com/img" + count +".jpg";
    imgLoader.load(imgRequest);
    imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);

    if(count == TOTAL_COUNT)
    {
          isCounting = false;
          count = -1;
    }
}

function onLoadedImg(e:Event):void
{
    imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);

    var bmp:Bitmap = e.currentTarget.content;
    bmp.x = Math.random() * width;
    bmp.y = Math.random() * height;
    bmp.width = 100;
    bmp.height = 100;
    this.addChild(bmp);

    imgBox.push(bmp);

    if( isCounting == false) 
    {
        return;
    }

    loadImage();
}

function unloadedImg(e:IOErrorEvent):void
{
    imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    trace("load Failed:" + e);
}

loadImage();

CASE2

var   imgLoader:Loader;
var   imgRequest:URLRequest;
const TOTAL_COUNT:int = 10;
var   imgBox:Array = [];

function loadImage2():void
{
    for(var i:int = 0; i<TOTAL_COUNT; i++)
    {
        imgLoader = new Loader();
        imgRequest = new URLRequest();
        imgRequest.url = "www.myURL.com/img" + i +".jpg";
        imgLoader.load(imgRequest);
        imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
        imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
    }
}

function onLoadedImg(e:Event):void
{
    imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);

    var bmp:Bitmap = e.currentTarget.content;
    bmp.x = Math.random() * width;
    bmp.y = Math.random() * height;
    bmp.width = 100;
    bmp.height = 100;
    this.addChild(bmp);

    imgBox.push(bmp);
}

function unloadedImg(e:IOErrorEvent):void
{
    imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    trace("load Failed:" + e);
}

loadImage2();

如果你想访问加载的图像。参考以下代码。如果不把它们放在一个数组中,以后就不能访问了。

for(var int:i=0; i<TOTAL_COUNT; i++)
{
    var bitmap:Bitmap = imgBox[i] as Bitmap;
    trace("index: " + i + "x: " + bitmap.x + "y: " + bitmap.y, "width: " + bitmap.width + "height: " + bitmap.height);
}
于 2012-08-09T04:56:19.120 回答
1

现在我们在同一页面上:

var imgArray:Array = new Array;
var totalImages:int = 42;
var totalLoaded:int = 0;
var loaded:Boolean = false;

function loadImages():void{
  for(var count:int = 0; count < totalImages; count++){
    var image:Loader = new Loader();
    image.load(new URLRequest("image" + i + ".jpg");
    image.addEventListener(Event.COMPLETE, loaded);
    imgArray.push(image);
  }
}

function loaded(e:Event):void{
  totalLoaded++;
  if (totalLoaded == totalImages){
    loaded = true;
  }
}

function displayImages():void{
  if (loaded){
    for(var i:int = 0; i < imgArray.length(); i++){
      addChild(imgArray[i]);
    }
  }
}
于 2012-08-09T04:26:20.220 回答