使用LocalConnection我们可以很方便的实现Flex与Flash之间的通信,既可以从Flex发消息到Flash中,也可以从Flash发消息到Flex端。下面通过一个样例来演示使用方法。
效果图如下:
--- Flash端 movie.swf ---
代码如下 |
复制代码 |
//创建LocalConnection对象,接受flex消息
var localConn:LocalConnection = new LocalConnection();
//localConnt添加一个停止函数
localConn.stopMove = function() {
stop();
}
//localConnt添加一个继续函数
localConn.resumeMove = function() {
play();
}
//连接名为flexToSwf的 连接
localConn.connect("flexToSwf");
ball.onRelease = function() {
//发送消息给flex端
var sendLC:LocalConnection = new LocalConnection();
sendLC.send( "flashConn", "clickBall" );
}
|
--- Flex端 F.mxml ---
代码如下 |
复制代码 |
creationComplete="initApp()">
import flash.net.LocalConnection;
//创建一个发送消息的本地链接
private var sendConn:LocalConnection;
//创建一个接收消息的本地连接
private var receiverConn:LocalConnection
internal function initApp():void{
//初始化变量
sendConn = new LocalConnection();
receiverConn = new LocalConnection();
//LocalConnection接受函数调用的时候必须指定client属性
receiverConn.client = this;
//连接名为flashConn的连接
receiverConn.connect("flashConn");
}
//停止
internal function stopMove():void{
sendConn.send("flexToSwf","stopMove");
}
//继续
internal function doResume():void{
sendConn.send("flexToSwf","resumeMove");
}
//点击小球,这个函数被swf调用
public function clickBall():void{
sendConn.send("flexToSwf","stopMove");
}
]]>
|