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
本文介绍了一种使用Flex加载外部SWF文件的方法,并提供了详细的代码示例。通过该方法可以实现在Flex应用中加载由Flash CS3创建的SWF文件,并能处理加载过程中的各种事件。
6211

被折叠的 条评论
为什么被折叠?



