加栽完成后顺序不乱,建议一张一张加载。
[Actionscript3] 纯文本查看 复制代码 var url:Array = new Array("pic_1.jpg,pic_2.jpg,pic_3.jpg");
var loader:DisplayLoader = new DisplayLoader(url);
loader.addEventListener(Event.COMPLETE,completeHandler);
loader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
function progressHandler(event:Event):void
{
trace(event.target.percent + "%");
}
function completeHandler(event:Event):void
{
//加载完成***********
addChild(loader);
}
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
public class DisplayLoader extends Sprite
{
private var loader:Loader;
private var list:*;
private var index:int = 0;
private var total:int;
/*
* DisplayLoader
* @param url 外部显示对象地址(可以是XMLList、XML、Array、String)
*/
public function DisplayLoader(url:*):void
{
this.list = url;
if (list is XMLList||list is XML)
{
total = list.length();
load(list[index]);
}
if (list is Array)
{
total = list.length;
load(list[index]);
}
if (list is String)
{
load(list);
}
}
public function load(url:String):void
{
loader = new Loader ;
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
}
private function progressHandler(event:ProgressEvent):void
{
this.dispatchEvent(new Event(ProgressEvent.PROGRESS));
}
private function completeHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
addChild(loader);
if (list is XMLList||list is XML||list is Array)
{
next();
}
else
{
this.dispatchEvent(new Event(Event.COMPLETE));
}
//这里可以为每张图片命名储存查找
}
private function next():void
{
if (index<total-1)
{
index++;
load(list[index]);
}
else
{
this.dispatchEvent(new Event(Event.COMPLETE));
}
}
/*加载进度*/
public function get percent():Number
{
return Math.round(loader.contentLoaderInfo.bytesLoaded/loader.contentLoaderInfo.bytesTotal*100);
}
}
}
|