当前位置: 代码迷 >> Web前端 >> 转――ActionScript 3.0 从写类整理笔记(三)――
  详细解决方案

转――ActionScript 3.0 从写类整理笔记(三)――

热度:275   发布时间:2012-10-24 14:15:58.0
转――ActionScript 3.0 自写类整理笔记(三)――
ByteLoader类

原文章出至http://www.xiaos8.com/article.asp?id=89


该类的主要功能是把swf,jpg,png,gif等文件以字节的形式加载进来
以便于使用Loader.loadBytes方法,重复加载使用素材
如果图片格式为jpg,并且是渐进式格式jpeg,那么该类还可以帮助你边加载边显示

index.base.net.byteLoader类讲解:
基本功能按字节加载图片,swf等

构造函数
public function ByteLoader(url:String = "")
如果传入了参数url,则立即执行加载!

load  加载方法
public function load(_url:String):void
开始加载,_url是加载的地址

updata  更新数据方法
public function updata():void
更新缓冲区的可读字节

close  关闭方法
public function close():void
类使用完毕,清除所有无用的数据,也可以用来强行关闭数据流,停止下载

data  属性
public var data:ByteArray
返回加载的字节

url  属性
public var url:String
返回加载的url

isLoad  属性(只读)
public function get isLoad():Boolean
返回是否有数据在加载

ProgressEvent.PROGRESS  事件
加载的过程中调度,并附带加载情况

Event.COMPLETE  事件
加载完毕调度

代码:import index.base.net.ByteLoader;

var bl:ByteLoader = new ByteLoader;
bl.load("http://www.xiaos8.com/uploads/pro/50preso3a2.swf");
bl.addEventListener(Event.COMPLETE,completeFun);
bl.addEventListener(ProgressEvent.PROGRESS,progressFun);

function completeFun(e:Event):void{
       var loaderoader = new Loader;
       loader.loadBytes(bl.data);
       addChild(loader);
       bl.removeEventListener(Event.COMPLETE,completeFun);
       bl.removeEventListener(ProgressEvent.PROGRESS,progressFun);
       bl.close();
       bl = null;
}

function progressFun(erogressEvent):void{
       trace(e.bytesLoaded);
       //如果是渐进式格式的jpeg图片,那么在发布这个事件的时候读取字节,用Loader.loadBytes加载,就可以形成边加载边显示
}代码:package index.base.net{
      
       importflash.events.EventDispatcher;
       import flash.events.ProgressEvent;
       import flash.events.Event;
       import flash.utils.ByteArray;
       import flash.net.URLStream;
       import flash.net.URLRequest;
      
       public class ByteLoader extends EventDispatcher{
             
               public var url:String;
               public var data:ByteArray;
               private var stream:URLStream;
             
               public function ByteLoader(url:String = ""){
                       if(url != ""){
                               load(url);
                       }
               }
             
               //加载
               public function load(_url:String):void{
                       url = _url;
                       data = new ByteArray;
                       stream = new URLStream;
                       stream.load(new URLRequest(url));
                       stream.addEventListener(Event.COMPLETE,completeFun);
                       stream.addEventListener(ProgressEvent.PROGRESS,progressFun);
               }
             
               //加载中
               private function progressFun(erogressEvent):void{
                       if(stream.bytesAvailable == 0) return;
                       updata();
                       dispatchEvent(e);
               }
             
               //加载完成
               private function completeFun(e:Event):void{
                       stream.removeEventListener(Event.COMPLETE,completeFun);
                       stream.removeEventListener(ProgressEvent.PROGRESS,progressFun);
                       updata();
                       if(isLoad) stream.close();
                       dispatchEvent(e);
               }
             
               //更新数据
               public function updata():void{
                       if(isLoad) stream.readBytes(data,data.length);
               }
             
               //清除数据
               public function close():void{
                       if(isLoad) stream.close();
                       stream = null;
                       data = null;
               }
             
               //获取是否有数据在加载
               public function get isLoad():Boolean{
                       if(stream == null) return false;
                       return stream.connected;
               }
       }
}
  相关解决方案