Flex中加载Flash CS3制作的swf文件

Flex加载Flash CS3制作的swf文件, 看Demo

 

package myflex {
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.URLRequest;
	
	import mx.core.UIComponent;
	
	/**
	 * http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_MovieClips_7.html
	 * 
	 * dispatch Event.COMPLETE when swf load complete.
	 * dispatch HTTPStatusEvent.HTTP_STATUS when network is error.
	 * dispatch IOErrorEvent.IO_ERROR when load swf error.
	 */
	public class ExternalSwfLoader extends UIComponent {
		
		private var loader : Loader;
		
		private var visibleAfterLoaded : Boolean;
		
		private var progressHandle : Function;
		
		private var loadSwfComplete : Boolean = false;;
		
		public function ExternalSwfLoader(progressHandle:Function = null, visibleAfterLoaded:Boolean = true) {
			super();
			init(progressHandle, visibleAfterLoaded);
		}
		
		protected function init(progressHandle:Function = null, visibleAfterLoaded:Boolean = true) : void {
			this.visibleAfterLoaded = visibleAfterLoaded;
			this.progressHandle = progressHandle;
			unLoad();
		}
		
		public function get content() : DisplayObject {
			if (loadSwfComplete) {
				return loader.content;
			}
			return null;
		} 
		
		public function get movieClip() : MovieClip {
			if (this.content) {
				return this.content as MovieClip;
			}
			return null;
		}
		
		public function loadSwf(externalSwfUrl:String, progressHandle:Function = null, visibleAfterLoaded:Boolean = true) : void {
			init(progressHandle, visibleAfterLoaded);
			
			//"http://www.[yourdomain].com/externalSwf.swf"
			var request:URLRequest = new URLRequest(externalSwfUrl);
			loader = new Loader();
			addListeners(loader.contentLoaderInfo);
			loader.load(request);
			
			if (visibleAfterLoaded) {
				addChild(loader);
			}
		}
		
		private function addListeners(dispatcher:IEventDispatcher) :void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            if (progressHandle != null) {
				dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandle);
            }
		}

		/**
		 *	load external swf successful.
		 */
		private function completeHandler(event:Event) : void {
			this.loadSwfComplete = true;
			this.width = loader.content.width;
			this.height = loader.content.height;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function httpStatusHandler(event:Event) : void {
			dispatchEvent(new Event(HTTPStatusEvent.HTTP_STATUS));
		}
		
		private function ioErrorHandler(event:Event) : void {
			dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
		}
		
		public function unLoad() : void {
			if (loadSwfComplete) {
				try {
					loader.unload();
					if (visibleAfterLoaded) {
						removeChild(loader);
					}
					loader = null;
					this.parent.removeChild(this);
				} catch (e:Error) {
					
				}
				loadSwfComplete = false;
			}
		}

	}
}
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute"
	width="400" height="250" 
	xmlns:myflex="myflex"
	backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FBD6B5, #793503]">
	
	<mx:Script>
		[CDATA[

			import mx.controls.Alert;
			import myflex.ExternalSwfLoader;

			[Bindable]
			private var externalSwfLoader : ExternalSwfLoader;
		
			private var flashMovieClip : MovieClip;
			
			private var externalSwfUrl : String = "flashbutton.swf";
			
			public function loadFlashSwf() : void {
				if (externalSwfLoader == null) {
					externalSwfLoader = new ExternalSwfLoader();
					externalSwfLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoErrorHandle);
					externalSwfLoader.addEventListener(Event.COMPLETE, onCompleteHandle);
					mainContainer.addChild(externalSwfLoader);
				}
				externalSwfLoader.loadSwf(externalSwfUrl);
				
				externalSwfLoader.setStyle("horizontalCenter", 0);
				externalSwfLoader.setStyle("verticalCenter", 0);
			}
			
			private function onCompleteHandle(e:Event) : void {
				try {
					if (externalSwfLoader.content != null) {
						flashMovieClip = externalSwfLoader.content as MovieClip;
						addListenersForMovieClip(flashMovieClip);
						
						Alert.show("Load swf successful. please click button in swf.");
					}
				} catch (e:Error) {
					Alert.show(e.message);
				}
			}
			
			private function onIoErrorHandle(e:Event) : void {
				Alert.show("IO_ERROR");
			}
			
			private function addListenersForMovieClip(movieClip : MovieClip) : void {
				movieClip.addEventListener("EventGoTOParent", onTestEventHandle);
			}
			
			private function onTestEventHandle(e:Event) : void {
				Alert.show("get flash swf event!");
			}
			
			public function sendTextToSwf() : void {
				try {
					if (externalSwfLoader != null && externalSwfLoader.content != null) {
						flashMovieClip.setMenu(flashtext.text);
					}
				} catch (e:Error) {
					Alert.show(e.message);
				}
			}
			
		]]
	</mx:Script>
	
	<mx:ApplicationControlBar left="15" top="15" right="15" fillAlphas="[1.0, 0.11]" fillColors="[#B2B1B1, #FDFCFC]" cornerRadius="8">
		<mx:LinkButton label="Load Swf" click="loadFlashSwf()"/>
		<mx:Spacer width="100%"/>
		<mx:TextInput id="flashtext" width="110"/>
		<mx:LinkButton label="Send text to Swf" click="sendTextToSwf()"/>
	</mx:ApplicationControlBar>
	
	<mx:Canvas id="mainContainer" top="56" bottom="10" left="15" right="15">
	</mx:Canvas>
	
</mx:Application>

 

参考

http://hi.baidu.com/ajamos/blog/item/70dca63b77e782ed15cecbc5.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值