Flex4_与Flash通信
方法一:
使用LocalConnection对象,据说不受AS版本限制。
1、Flash端,在需要通信的位置插入代码:
var localConn:LocalConnection=new LocalConnection(); //lc_from_flash:是建立连接的字符串,该字符串必须与Flex中connect()方法中所需参数一致。 //ShowDetails:为Flex提供的Public方法。 //5:ShowDetails方法的数值参数 localConn .send("lc_from_flash","ShowDetails",5);
2、Flex端
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; private var flashMc:MovieClip; private function FlashComplete(event:Event):void{ InitApp(event as FlexEvent); } private var _localConnection:LocalConnection; protected function InitApp(event:FlexEvent):void { _localConnection=new LocalConnection(); _localConnection.connect("lc_from_flash"); //与Flash一致的连接串 _localConnection.client=this; } //方法名,与参数与Flash一致 public function ShowDetails(param:Number):void{ Alert.show(param.toString()); } ]]> </fx:Script> <mx:Image x="111" y="88" source="but.swf" complete="FlashComplete(event)" id="flashContent"/> </s:Application>