论坛首页 编程语言技术论坛

【原创】自定义的Flex加载Flash元件库工具,共享一下

浏览 3100 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-06-30   最后修改:2010-09-08

    总是加载东西觉得麻烦,封装了一个加载类,共享一下。

 /**
     *Author:齐齐兽
	 *Function:Load external swf file
	 *   
	 **/
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	[Event(name='loaded',type='loaded')]
	[Event(name='progress',type='progress')]
	public class LoaderTool
	{
		private var _assetsURL:String;
		private var urlLoader:URLLoader=new URLLoader;
		private var loader:Loader=new Loader;
		private var _result:Object;
		private var _percent:int;
		public static const dispatcher:EventDispatcher=new EventDispatcher();

		public function LoaderTool(assetsURL:String)
		{
			this._assetsURL=assetsURL;
			urlLoader.dataFormat=URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, urlLoaderComplete);
			urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
		}

		public function start():void
		{
			urlLoader.load(new URLRequest(this._assetsURL));
		}

		private function urlLoaderComplete(event:Event):void
		{
			loader.loadBytes(this.urlLoader.data as ByteArray);
		}

		private function onComplete(event:Event):void
		{
			this._result=event.currentTarget;
			LoaderTool.dispatcher.dispatchEvent(new Event('loaded'));
		}

		private function onProgress(event:Event):void
		{
			_percent=urlLoader.bytesLoaded / urlLoader.bytesTotal * 100
			LoaderTool.dispatcher.dispatchEvent(new Event('progress'));
		}

		public function get result():Object
		{
			return this._result;
		}

		public function get percent():int
		{
			return this._percent;
		}
	}

 

采用二进制二次加载方法,为的是避开安全沙箱。加载完毕派发loaded事件;如果需要播放加载函数,监听progress事件即可,使用percent,返回的是0-100的整数。

 

使用示例:

private var UIloadTool:LoaderTool=new LoaderTool(ui_URL);
this.UIloadTool.start(); //加载UI库
LoaderTool.dispatcher.addEventListener('loaded', UILoaded);
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics