当前位置:软件学习 > Flash >>

as 加载Loader.loadBytes方法

该类的主要功能是把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{
       
        import flash.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;
                }
        }
}

补充:flash教程,As3.0
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,