Flex运行时动态加载swf

加载外部SWF文件,并读取其中的组件和类。其中借鉴了fluorida里的部分代码。

 

//ActionScript code
	public class SWFLoaderWrapper {
		
		private var _loader : SWFLoader;
		
		public function SWFLoaderWrapper(swfLoader : SWFLoader) {
			_loader = swfLoader;
		}

		public function getSWFApplication() : Application {
			if(_loader == null || _loader.content == null) {
				return null;	
			}
			var sysManager : SystemManager = _loader.content as SystemManager;
			if(sysManager == null) {
				return null;	
			}
			return sysManager.application as Application;
		}
		
		private function recursiveSelect(componentId : String, container:DisplayObjectContainer, result:Array) : void {
			selectChildren(componentId, container, result);
			for(var cursor:int = 0; cursor < container.numChildren; cursor++) {
				var child:DisplayObject = container.getChildAt(cursor);
				var subContainer:DisplayObjectContainer = child as DisplayObjectContainer;
				if(subContainer == null) {
					continue;
				}
				recursiveSelect(componentId, subContainer, result);
			}
		}
		
		private function selectChildren(componentId : String, container:DisplayObjectContainer, result:Array) : void {
			for(var cursor:int = 0; cursor < container.numChildren; cursor++) {
				var child:DisplayObject = container.getChildAt(cursor);
				
				if (match(componentId, child)) {
					result.push(child);
				}
			}
		}
		
		private function match(componentId : String, element:DisplayObject) : Boolean {
			if(element.hasOwnProperty("id") && element["id"] == componentId) {
				return true;
			}
			if(element.name == componentId) {
				return true;
			}
			return false;
		}
		
		public function getComponentById(componentId : String) : DisplayObject {
			var result : Array = getComponents(componentId);
			if (result != null && result.length > 0) {
				return result[0];
			}
			return null;
		}
		
		public function getComponents(componentId : String) : Array {
			var result : Array = new Array();
			recursiveSelect(componentId, getSWFApplication(), result);
			return result;
		}
		
		public function getClassByName(className : String) : Class {
			if (getSWFApplication() == null) {
				return null;
			}
         	var clazz:Class = null;
         	try {
            	clazz = getSWFApplication().systemManager.loaderInfo.applicationDomain.getDefinition(className) as Class;
             	return clazz;
         	} catch (e:Error) {
            	throw new IllegalOperationError(className + " doesn't exist");
         	}
         	return clazz;
		}
	}

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	applicationComplete="initApp()"
	creationComplete="init()">
	
	<mx:Script>
		[CDATA[
			import mx.controls.Button;
			import mx.controls.Alert;
			import utils.SWFLoaderWrapper;
			
			private var _swfLoaderWrapper : SWFLoaderWrapper;
			
			public function initApp() : void {
				flash.system.Security.allowDomain("*");
			}
			
			public function init() : void {
				swfLoader.addEventListener(Event.INIT, initHandle);
			}
			
			private function initHandle(e : Event) : void {
				Alert.show("ok");
				_swfLoaderWrapper = new SWFLoaderWrapper(swfLoader);
			}
			
			public function loadSwf() : void {
				swfLoader.load(swfurl.text);
			}
			
			public function getComponentById() : void {
				var displayObject : DisplayObject = _swfLoaderWrapper.getComponentById(componentId.text);
				if (displayObject != null) {
					textArea.text = flash.utils.describeType(displayObject).toString();
				}
			}
			
			private function getClassByName() : void {
				var clazz : Class = _swfLoaderWrapper.getClassByName(classNameText.text);
				if (clazz != null) {
                     var obj:Object = new clazz();
                     textArea.text = flash.utils.describeType(obj).toString();
				}
			}
			
		]]
	</mx:Script>
	
	<mx:TextInput id="swfurl" left="10" right="294" top="10"/>
	<mx:Button x="10" y="40" label="Load Swf" click="loadSwf()"/>
	
	<mx:Label x="10" y="72" text="Component Id" fontSize="12"/>
	<mx:TextInput id="componentId" x="109" y="72" width="360"/>
	<mx:Button x="10" y="100" label="get Component by Id" click="getComponentById()"/>
	
	<mx:Label x="10" y="141" text="Class Name" fontSize="12"/>
	<mx:TextInput id="classNameText" x="109" y="141" width="360"/>
	<mx:Button x="10" y="169" label="get Class by Name" click="getClassByName()"/>
	
	<mx:TextArea id="textArea" top="199" left="10" right="294" height="139"/>
	<mx:SWFLoader id="swfLoader" left="10" right="294" top="346" bottom="10"/>
	
</mx:Application>

 

 

1.尝试动态加载使用了RSL的swf,出错,还不知道怎么解决。

2.不能加载swc,遗憾。

3.通过flash.utils.describeType只能看到public的属性和方法。

4.也可以只使用flash.display.Loader来动态加载swf.  http://www.cnblogs.com/jssy/archive/2007/09/09/887815.html

你可能感兴趣的:(Flex,Flash,360,Adobe,actionscript)