其实就是Flex将Flash以资源的形式加载到Flex项目中,然后再获取该资源的内容对象,这样就可以操作该资源中对象的帧代码和文档类代码的公共方法了。
同时Flex也可以接收Flash发出的事件,从而实现相互通信的数据交互。
1、首先使用flash cs3建一个as 3的flash文件,建二帧,在第一帧用文本工具写上Hello World,并在第一帧上写上如下代码:
HelloWorld!
- stop();
- function gonext():void
- {
- gotoAndPlay(2);
- }//定义一个gonext的函数,作用为跳到第二帧;
stop();
function gonext():void
{
gotoAndPlay(2);
}//定义一个gonext的函数,作用为跳到第二帧;
2、在第二帧上用文本工具写上:
“我是大将军”的文本
再制作一个调用flex函数是按钮button,命名为ozbutton,
然后写下如下代码:
- stop();
- ozbutton.addEventListener(MouseEvent.CLICK,OzClick);
- function OzClick(evt:MouseEvent):void
- {
- dispatchEvent(new Event("OKTWO"));
- }//为ozbutton添加一个监听鼠标点击的事件,当鼠标点击时,发送一个OKTWO的广播事件
stop();
ozbutton.addEventListener(MouseEvent.CLICK,OzClick);
function OzClick(evt:MouseEvent):void
{
dispatchEvent(new Event("OKTWO"));
}//为ozbutton添加一个监听鼠标点击的事件,当鼠标点击时,发送一个OKTWO的广播事件
3、这样flash的制做就完成了,命名为flash;
flash
[olor=orange]下面说下在flex里如何调用[/color]
4、首先建一个名为flex_flash的flex工程,用一个image的组件来加载刚制做的flash文件;
- <mx:Image x="111" y="88" source="flash/flash.swf" complete="FlashComplete(event)" id="flashContent"/>
<mx:Image x="111" y="88" source="flash/flash.swf" complete="FlashComplete(event)" id="flashContent"/>
5、当flash加载完成之后会触发complete事件,并调用FlashComplete(event)函数;
- private function FlashComplete(event:Event):void
- {
- flashMc=flashContent.content as MovieClip;
- flashMc.addEventListener("OKTWO",SayHello);
-
- }
private function FlashComplete(event:Event):void
{
flashMc=flashContent.content as MovieClip;
flashMc.addEventListener("OKTWO",SayHello);
}
6、flashMc是一个MovieClip类,并把flashContent.content赋值给它,这样flashMc就相当于你刚制做的flash,你可以使用它来调用里面的函数;完整的代码如下
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
- <mx:Script>
- <![CDATA[
- import mx.controls.Button;
- import mx.controls.Alert;
- private var flashMc:MovieClip
- private function FlashComplete(event:Event):void
- {
- flashMc=flashContent.content as MovieClip;
- flashMc.addEventListener("OKTWO",SayHello);
-
- }
- private function NextFrame():void
- {
- flashMc.gonext();
-
- }
-
- private function SayHello(event:Event):void
- {
- Alert.show("HELLO World");
- }
- ]]>
- </mx:Script>
- <mx:Image x="111" y="88" source="flash/flash.swf" complete="FlashComplete(event)" id="flashContent"/>
- <mx:Button x="182" y="222" label="NextFrame" click="NextFrame()" id="b"/></mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Button;
import mx.controls.Alert;
private var flashMc:MovieClip
private function FlashComplete(event:Event):void
{
flashMc=flashContent.content as MovieClip;
flashMc.addEventListener("OKTWO",SayHello);
}
private function NextFrame():void
{
flashMc.gonext();
}
private function SayHello(event:Event):void
{
Alert.show("HELLO World");
}
]]>
</mx:Script>
<mx:Image x="111" y="88" source="flash/flash.swf" complete="FlashComplete(event)" id="flashContent"/>
<mx:Button x="182" y="222" label="NextFrame" click="NextFrame()" id="b"/></mx:Application>