as3.0针对两个独立swf的通信有个专门的类-----LocalConnection
既然是两个独立的swf进行通信,那么就将两个swf的名字暂叫:A(派发器),B(接收器)。查阅as3.0帮助后,用了下他们提供的例子,还挺好用,在此和大家分享下。
A的源码:
// Code in LocalConnectionSenderExample.as
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.LocalConnection;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.StatusEvent;
import flash.text.TextFieldAutoSize;
public class LocalConnectionSenderExample extends Sprite {
private var conn:LocalConnection;
// UI elements
private var messageLabel:TextField;
private var message:TextField;
private var sendBtn:Sprite;
public function LocalConnectionSenderExample() {
buildUI();
sendBtn.addEventListener(MouseEvent.CLICK, sendMessage);
conn = new LocalConnection();
conn.addEventListener(StatusEvent.STATUS, onStatus);
}
private function sendMessage(event:MouseEvent):void {
//conn.send("A和B链接标示","A调用B中的方法","A与B商量好的传输对象");
conn.send("myConnection","lcHandler", message.text);
}
private function onStatus(event:StatusEvent):void {
switch (event.level) {
case "status":
trace("LocalConnection.send() succeeded A向B派发成功");
break;
case "error":
trace("LocalConnection.send() failed A向B派发失败");
break;
}
}
private function buildUI():void {
const hPadding:uint = 5;
// messageLabel
messageLabel = new TextField();
messageLabel.x = 10;
messageLabel.y = 10;
messageLabel.text = "Text to send:";
messageLabel.autoSize = TextFieldAutoSize.LEFT;
addChild(messageLabel);
// message
message = new TextField();
message.x = messageLabel.x + messageLabel.width + hPadding;
message.y = 10;
message.width = 120;
message.height = 20;
message.background = true;
message.border = true;
message.type = TextFieldType.INPUT;
addChild(message);
// sendBtn
sendBtn = new Sprite();
sendBtn.x = message.x + message.width + hPadding;
sendBtn.y = 10;
var sendLbl:TextField = new TextField();
sendLbl.x = 1 + hPadding;
sendLbl.y = 1;
sendLbl.selectable = false;
sendLbl.autoSize = TextFieldAutoSize.LEFT;
sendLbl.text = "Send";
sendBtn.addChild(sendLbl);
sendBtn.graphics.lineStyle(1);
sendBtn.graphics.beginFill(0xcccccc);
sendBtn.graphics.drawRoundRect(0, 0, (sendLbl.width + 2 + hPadding + hPadding), (sendLbl.height + 2), 5, 5);
sendBtn.graphics.endFill();
addChild(sendBtn);
}
}
}
B的源码:
// Code in LocalConnectionReceiverExample.as
package {
import flash.display.Sprite;
import flash.net.LocalConnection;
import flash.text.TextField;
public class LocalConnectionReceiverExample extends Sprite {
private var conn:LocalConnection;
private var output:TextField;
public function LocalConnectionReceiverExample() {
buildUI();
conn = new LocalConnection();
conn.client = this;//this:建立连接的对象
try {
conn.connect("myConnection");
} catch (error:ArgumentError) {
trace("Can't connect...the connection name is already being used by another SWF 无法连接...连接的名称已经被另一个SWF使用");
}
}
/**
* 被调用的方法
* @param msg 传输的对象
*/
public function lcHandler(msg:String):void {
output.appendText(msg + "\n");
}
private function buildUI():void {
output = new TextField();
output.background = true;
output.border = true;
output.wordWrap = true;
addChild(output);
}
}
}
注意:他们之间的是需要一个connect连接的,一个connect可以多个A派发,但是接收方只能有一个。意思也可以这么说,A可以打开多个窗口,但是B只能打开一个,如果B打开多个,也只能响应第一个打开的。
ps: 例子就是这么个例子,代码就是这么个代码。 至于你怎么扩展功能,那就看你水平了!嘿嘿……
相关推荐
一个localConnection的例子
NULL 博文链接:https://globle.iteye.com/blog/748144
该案例中TestLocalConnection为主SWF文件加载了TestLocalConnectionReceiver.SWF,两者之间是在同一个域下的通信,工程代码编译路径需要下载者手动修改下。其他技术细节参见Flex_API中的LocalConnection章节。本机...
一个关于flex 中flash文件通信的说明。actionscript3.0
多个swf的通讯,使用 LocalConnection 类可以创建一个 LocalConnection 对象,该对象可在一个 SWF 文件中或多个 SWF 文件间,调用另一个 LocalConnection 对象中的方法。
flash localconnection问题源文件
LocalConnection的问题
在两个应用程序之间发送消息 连接到不同域中的内容和AIR应用程序 第章:与AIR中的本机进程通信 本机进程通信概述 启动和关闭本机进程 与本机进程通信 本机进程通信的安全性注意事项 第章:使用外部API 使用外部API的...
About this manual 13 ...Setting LocalConnection permissions.790 Controlling access to scripts in a host web page..790 Shared objects...792 Camera, microphone, clipboard, mouse, and keyboard access.794
Chapter 3 - Introduction to ActionScript 3.0 Chapter 4 - Using External API and LocalConnection Chapter 5 - Working with XML Chapter 6 - Overview of LiveCycle Data Services and BlazeDS Chapter...
as3 加载 as2的通讯,flex调用 as2的
第1篇ActionScript3.0语言基础 第1章ActionScript3.0概述 2 1.1ActionScript概述 2 1.1.1ActionScript环境 2 1.1.2ActionScript3.0特性 3 1.1.3ActionScript3.0代码组织 5 1.2ActionScript3.0API概览 5 1.3小结 8 第...
/*实现客户端不同域页面间Javascript相互调用(异步)的静态类--虫洞插件0.1版*/ feature: 1)纯前台插件,不需要后台配置,FLASH PLAYER版本要求在10.0以上. 2)本插件是基于同一台电脑上的FLASH之间能进行...
花了两个晚上简略的研究了下,LocalConnection的单向通信非常的简单,不过要实现多个终端交互,必须自己实现一套消息机制,见智见仁了。 为了简单演示,本例使用了基于广播的观察者模式:每个终端可以订阅自己感...
利用iframe和location.hash,数据直接暴露在了url中,数据容量和类型都有限 3.Flash LocalConnection, 对象可在一个 SWF 文件中或多个 SWF 文件间进行通信, 只要 在同一客户端就行,跨应用程序, 可以跨域。...
##使用说明###简介通过flash进行同一浏览器跨页面,跨内核的通讯。...[removed]##文档参考###全局对象localConnect 必须有全局对象是为了让flash可调用###方法init(handle)初始化flash文件,参数handle为fla
构造函数 Array 类、Boolean 类、Camera 类、Color 类、ContextMenu 类、ContextMenuItem 类、Date 类、Error 类、LoadVars 类、LocalConnection 类、Microphone 类、NetConnection 类、NetStream 类、Number 类、...
(或SyncConnection / LocalConnection ) 使用Message发送和接收消息。 获取和附加所有类型的,有关详细信息,请参见。 使用dbus-crossroads或dbus-tree板条箱的构建方法调度服务器。 支持标准D-Bus接口(自检,...
LocalConnection and DataProvider 使用方法範例