Flex 加载资源、Skin 等文件,并动态生成对象

private var symbolLoader:Loader = null;
private var symbolRequest:URLRequest = null;

private function loadTeamSymbol():void{
	symbolLoader = new Loader();
	symbolLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, symbolLoader_CompleteHandler);
	symbolRequest = new URLRequest(SourceImagesPath.teamSymbolPath + "teamSymbol.swf");
	var loaderContext:LoaderContext = new LoaderContext(true);
	loaderContext.applicationDomain = ApplicationDomain.currentDomain;
	symbolLoader.load(symbolRequest, loaderContext);
}

private function symbolLoader_CompleteHandler(event:Event):void{
	//var teamSymbolClass:Class = getDefinitionByName("TeamSymbol") as Class;
	//var teamSymbolClass:Class = ApplicationDomain.currentDomain.getDefinition("TeamSymbol") as Class;
	var teamSymbolClass:Class = symbolLoader.contentLoaderInfo.applicationDomain.getDefinition("TeamSymbol") as Class;
	Cache.teamSymbol = new teamSymbolClass();
}

 

import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import game.com.tool.MsgBox;

	public class LoadExternalRes
	{	
		private static var currObj:LoadExternalRes = new LoadExternalRes();
		
		private var externalResLoader:Loader = null;
		private var externalResRequest:URLRequest = null;
		
		public function LoadExternalRes()
		{
		}
		
		public static function instance():LoadExternalRes{
			return currObj;
		}
		
		/**
		 *加载外部资源 
		 * @param path
		 */
		public function loadExternalResources(path:String):void{
			externalResLoader = new Loader();
			externalResLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, externalResLoader_CompleteHandler);
			externalResLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, externalResLoader_IoErrorHandler);
			externalResLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, externalResLoader_SecurityErrorHandler);
			externalResRequest = new URLRequest(path);
			var loaderContext:LoaderContext = new LoaderContext(true);
			loaderContext.applicationDomain = ApplicationDomain.currentDomain;
			externalResLoader.load(externalResRequest, loaderContext);
		}
		
		private function externalResLoader_CompleteHandler(event:Event):void{
			//加载完成
		}
		
		private function externalResLoader_IoErrorHandler(event:IOErrorEvent):void{
			var loaderUrl:String = (event.currentTarget as LoaderInfo).loaderURL;
			MsgBox.newMsgBoxByContent("IoError:" + loaderUrl + "加载错误...");
		}
		
		private function externalResLoader_SecurityErrorHandler(event:SecurityErrorEvent):void{
			var loaderUrl:String = (event.currentTarget as LoaderInfo).loaderURL;
			MsgBox.newMsgBoxByContent("SecurityError:" + loaderUrl + "加载错误...");
		}
	}

 

你可能感兴趣的:(Flex)