BulkLoader,很好用的队列加载类

本文介绍了一个使用Flash进行资源批量加载的例子,包括图片、配置文件、音频及视频等不同类型的资源加载方式。文中还展示了如何设置加载项的优先级、最大尝试次数等高级选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

var loader : BulkLoader = new BulkLoader("main");
loader.add("bg.jpg");
loader.add("config.xml");
loader.add("soundtrack.mp3");
loader.add("intro.flv");
loader.addEventListener(BulkLoader.PROGRESS, onProgress);
loader.addEventListener(BulkLoader.COMPLETE, onComplete);
loader.start();
 
function onProgress(evt : BulkProgressEvent) : void{
    trace(evt.percentLoaded);
}
 
function onComplete(evt : Event) : void{
    var bgBitmap = loader.getBitmap("bg.jpg");
    addChild(bgBitmap);
    var video : Video = new Video();
    video.attachNetStream(loader.getNetStream("intro.flv"));
    parseConfig(loader.getXML("config.xml"));
}
//---------------------------------------------------------------------------
// simplest case:
loader.add("logo.png");
 
// use an "id" so the item can be retrieved later without a reference to the url
loader.add("background.jpg", {id:"bg"});
 
// since the url by itself can't tell us what the filetype is, use the type property to let BulkLoader know what to do:
loader.add("/some-web-services?size=Large", {type:"image"});
 
// add an item that should be loaded first (higher priority):
loader.add("/data/config.xml", {priority:20});
 
// add a max try number (defaults to 3)
loader.add("/unreliable-web-services.xml", {maxTries:6});
 
// you can also use a URLRequest object , this will load from a POST request
 
var postRequest : URLRequest = new URLRequest("/save-prefs.php");
postRequest.method = "POST";
 
var postData : URLVariables = new URLVariables(myPostDataObject );
postRequest.data = postData;
loader.add(postRequest, {"id":"settings"});
 
// of course, all options can be combined:
loader.add("the-sound-webservices?name=maintrack", {"id":"soundtrack", type:"sound", maxTries:1, priority:100});
//-------------------------------------------------------------------------------------------

// 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("data/config.xml").addEventListener(BulkLoader.ERROR, onXMLFailed);

//----------------------------------------------------------

var theBgBitmap : Bitmap = loader.getContent("bg") as Bitmap;
 
// you don't need to keep a reference to the loader intance, you can get it by name:
var theBgBitmap : Bitmap = BulkLoader.getLoader("main-site").getContent("bg") as Bitmap;
 
// you can also use the conviniece methods to get a typed object:
var theBgBitmap : Bitmap = loader.getBitmap("bg");
 
// grab a BitmapData directly:
var theBgBitmap : Bitmap = loader.getBitmapData("bg");

转载于:https://www.cnblogs.com/blueheart104/archive/2010/03/08/1680553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值