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;
			}
		}

	}
}
 


	
	
		[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);
				}
			}
			
		]]
	
	
	
		
		
		
		
	
	
	
	
	

 

参考

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

你可能感兴趣的:(Flex,Flash,Adobe,HTML,.net)