BulkLoader是一个不错且使用简单的开源加载类,其能实现对不同类型文件的加载进行管理。
在尝试加载多个文件时候只要使用 add + 文件地址,在加载结束后使用 “get文件类型”+文件地址或加载时候注册的id去获得要得到的文件。
下载地址: http://code.google.com/p/bulk-loader/
官方:http://www.stimuli.com.br/trane/2007/nov/25/loading-reloaded/
链接:https://pan.baidu.com/s/1e9kF-llTHARZy4zSx18Q3Q 密码:1l29
package {
import br.com.stimuli.loading.BulkLoader;
import br.com.stimuli.loading.BulkProgressEvent;
import flash.events.*;
import flash.display.*;
import flash.media.*;
import flash.net.*;
public class SimpleExampleMain extends MovieClip {
public var loader:BulkLoader;
public var v:Video;
public var counter:int=0;
public function SimpleExampleMain() {
//构建BulkLoader的时候需要给它一个名称
loader=new BulkLoader("main-site");
//设置输出日志
loader.logLevel=BulkLoader.LOG_INFO;
//构建好了以后,通过add方法往队列里添加需要加载的对象
loader.add("photo.png");
//添加加载对象时候,也可以给它添加一个id,方便以后调用
loader.add("images.jpg", {id:"bg"});
//还可以通过priority属性调整加载对象的加载顺序,priority值越大,优先权越高,越早加载
loader.add("list.xml", {priority:20, id:"config-xml"});
//加载一个动画,加载动画的时候可以用pausedAtStart属性暂停播放动画
loader.add("mov.fla", {maxTries:6, id:"the-video", pausedAtStart:true});
//maxTries属性用于设定加载失败时的重试次数,注意,这里的“id”用了字符串命名
loader.add("song.mp3", {"id":"soundtrack", maxTries:1, priority:100});
//看了最新版本的文档,已经开始支持swf和json,一阵欣喜。
//添加一个COMPLETE事件,这个事件会在队列里的所有对象都加载完毕后触发
loader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded);
//添加一个PROGRESS事件,这个事件会在队列加载时不断触发。通常可以用于监听加载进度。
loader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress);
//队列编辑完毕后用star方法开始加载队列
loader.start();
}
public function onAllItemsLoaded(evt : Event):void {
trace("every thing is loaded!");
//建立一个Video对象
var video : Video = new Video();
//从队列里提取刚才加载的视频流
var theNetStream:NetStream=loader.getNetStream("the-video");
addChild(video);
video.attachNetStream(theNetStream);
theNetStream.resume();
video.y=300;
//提取图片
//可以直接通过url提取对象
var bitmapCats:Bitmap=loader.getBitmap("photo.png");
bitmapCats.width=200;
bitmapCats.scaleY=bitmapCats.scaleX;
addChild(bitmapCats);
//当然,也可以通过id提取对象
var bitmapShoes:Bitmap=loader.getBitmap("bg");
bitmapShoes.width=200;
bitmapShoes.scaleY=bitmapShoes.scaleX;
bitmapShoes.x=220;
addChild(bitmapShoes);
//提取音频
var soundtrack:Sound=loader.getSound("soundtrack");
soundtrack.play();
//提取一个xml文档
var theXML:XML=loader.getXML("config-xml");
trace(theXML);
}
//通过BulkProgressEvent的loadingStatus方法可以显示加载过程中的所有信息!
public function onAllItemsProgress(evt : BulkProgressEvent):void {
trace(evt.loadingStatus());
}
}
}
侦听事件。比较厉害的是可以通过加载时候注册的id或文件地址来侦听想要侦听的文件。
// attaching events to all items:
// this will fire once all items have been loaded
loader.addEventListener(BulkLoader.COMPLETE, onAllLoaded);
// this will fire on progress for any item
// the event , BulkProgress is a subclass of ProgressEvent (with extra information)
loader.addEventListener(BulkLoader.PROGRESS, onAllProgress);
// this will fire if any item fails to load:
// the event is BulkErrorEvent and holds an array (errors) with all failed LoadingItem instances
loader.addEventListener(BulkLoader.ERROR, onAllError);
// you can also listen to events in individual items
// this will fire as soon as the item registred with the id of "bg" is done loading (even if there are other items to load)
loader.get("bg").addEventListener(Event.COMPLETE,onBackgroundLoaded)
// this will only trigged if the config.xml loading fails:
loader.get("list.xml").addEventListener(BulkLoader.ERROR, onXMLFailed);
获取文件方式:
// attaching events to all items:
// this will fire once all items have been loaded
loader.addEventListener(BulkLoader.COMPLETE, onAllLoaded);
// this will fire on progress for any item
// the event , BulkProgress is a subclass of ProgressEvent (with extra information)
loader.addEventListener(BulkLoader.PROGRESS, onAllProgress);
// this will fire if any item fails to load:
// the event is BulkErrorEvent and holds an array (errors) with all failed LoadingItem instances
loader.addEventListener(BulkLoader.ERROR, onAllError);
// you can also listen to events in individual items
// this will fire as soon as the item registred with the id of "bg" is done loading (even if there are other items to load)
loader.get("bg").addEventListener(Event.COMPLETE,onBackgroundLoaded)
// this will only trigged if the config.xml loading fails:
loader.get("list.xml").addEventListener(BulkLoader.ERROR, onXMLFailed);