`
hz_chenwenbiao
  • 浏览: 996568 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Flex运行时动态加载swf(转)

    博客分类:
  • flex
阅读更多

加载外部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

分享到:
评论

相关推荐

    Flex3.0设备字体,字体嵌入

    进SWF文件中或者运行时加载到SWF文件中,供SWF文件使用。这样不管用户是否安装了该字体, 都可以正常显示。使用嵌入字体的好处还有:文本具有抗锯齿,边缘更平滑,文本可以设置透明 度,文本还可以旋转。当然也有...

    flexchartdemo

    这个工程是用flex画图表(piechart饼状图/lineChart折线图/ColumnChart直方图),并将生产的swf文件成功嵌入到网页中,在你自己的网页中你想放的任意地方嵌入swf(flash动画)文件。 亮点: 1、三个图鼠标放在相应...

    flex modules 中英文版

    SWF文件越大加载时间越长 示例程序基于Flex 2文档中的一个程序,但是我将它做了一些更改来说明几个常见的问题。这个例子展示了一个主程序和其它三个共享公有数据的模块(Module)。 其中一个设计要素是一个接口...

    flex3的cookbook书籍完整版dpf(包含目录)

    运行时加载CSS 9.7节.运行时声明样式 9.8节.自定义组件样式属性 9.9节.同一个程序中使用多个主题 9.10节.编译主题SWC 9.11节.应用嵌入字体 9.12节.从SWF文件中嵌入字体 9.13 节. 嵌入图像的皮肤 9.14节.从SWF文件中...

    UCenter1.6头像上传破解版源码(uc_camera_v1.0)

    locale.xml 本地化语言(加载camera.swf时会从Web端读取) crossdomain.xml flash域安全配置(如上) index.php Web服务器端执行返回html页面以供浏览器解析加载camera.swf 3.编译swf与部署Web端: 运行测试头像...

    在Flex中发布地图 程序报错 SecurityError: Error #2048: 安全沙箱冲突,只需要下载添加这个就可以

    直接运行没错误,但是当发布时,访问就错了,提示SecurityError: Error #2048: 安全沙箱冲突:http://localhost:8086/index.swf 不能从 http://www-c8d8bc651c4/ArcGIS/rest/services/zhengzhou/MapServer?f=json ...

    ActionScript开发人员指南中文版

    使用Flash运行时的调试版 在应用程序中处理同步错误 创建自定义错误类 响应错误事件和状态 比较错误类 处理错误示例:CustomErrors应用程序 第章:使用正则表达式 正则表达式基础知识 正则表达式语法 对字符串使用...

    flexmojos-compatible-model-validator

    FlexMojos 兼容模型验证器。 FlexMojos 提供了 6 个范围用作依赖项的链接: ... 仅与 Flex 和 AIR 应用程序(不是 SWC 或 ActionScript 3 项目)相关,这可确保 SWF 引用的库将在运行时一次加载到用户的 Fl

    ActionScript开发技术大全

    28.1AdobeAIR运行时安装与配置 584 28.2使用FlashCS3IDE创建和部署AIR应用 585 28.2.1安装FlashCS3IDEupdate9.0.3 585 28.2.2创建和部署AIR应用 585 28.3使用FlexBuilder3创建和部署AIR应用 588 28.4AIR应用配置文件...

    asp.net知识库

    完整的动态加载/卸载程序集的解决方案 从NUnit中理解.NET自定义属性的应用(转载) 如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件机制的简单实现 我对J2EE和.NET的一点理解 难分难舍的DSO(一) ...

Global site tag (gtag.js) - Google Analytics