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

 

源自:http://wangcheng.iteye.com/category/12986

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值