[ActionScript 3.0] LocalConnection示例
下例包含两个 ActionScript 类,这两个类应当编译到两个单独的 SWF 文件中:
在 LocalConnectionSenderExample SWF 文件中,将创建 LocalConnection 实例,并且当按下按钮时使用 call() 方法通过名为“myConnection”的连接调用 SWF 文件中名为 lcHandler 的方法,调用时将 TextField 的内容作为参数传递。
在 LocalConnectionReceiverExample SWF 文件中,将创建 LocalConnection 实例并调用 connect() 方法,将此 SWF 文件指定为发送到名为“myConnection”的连接的消息的接收方。此外,此类还包括一个名为 lcHandler() 的公共方法,此方法就是 LocalConnectionSenderExample SWF 文件调用的方法。调用时,作为参数传入的文本将被追加到舞台的 TextField 中。
注意:为了测试此示例,必须将两个 SWF 文件同时加载到同一台计算机上。
// 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("myConnection", "lcHandler", message.text);
} private function onStatus(event:StatusEvent):void {
switch (event.level) {
case "status":
trace("LocalConnection.send() succeeded");
break;
case "error":
trace("LocalConnection.send() failed");
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);
}
}
}
// 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;
try {
conn.connect("myConnection");
} catch (error:ArgumentError) {
trace("Can't connect...the connection name is already being used by another SWF");
}
} 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);
}
}
}
--------------------------------------------------------------------------------
[ActionScript 3.0] LocalConnection示例的更多相关文章
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...
- [ActionScript 3.0] AS3.0 动态加载显示内容
可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...
- ActionScript 3.0 API 中的 Video 类
注:这个类在Flash流媒体开发中使用的很频繁,在此记录一下它的使用方法. 包 flash.media 类 public class Video 继承 Video DisplayObject Ev ...
- [ActionScript 3.0] as3处理xml的功能和遍历节点
as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...
- [ActionScript 3.0] 正则表达式
正则表达式: 正则表达式最早是由数学家Stephen Kleene在对自然语言的递增研究成果的基础上,于1956提出来的.具有完整语法的正则表达式,主要使用在字符串的格式的匹配方面上,后来也逐渐应用到 ...
- 在 Flash ActionScript 2.0 中调用 Javascript 方法
本篇文章由:http://xinpure.com/call-the-javascript-method-in-flash-actionscript-2-0/ 在 Flash ActionScript ...
- ActionScript 3.0 for the Lunder Algorithm
package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...
- 【转】学习Flex ActionScript 3.0 强烈推荐电子书
学习Flex ActionScript 3.0 强烈推荐电子书 AdvancED ActionScript 3.0 Animation(<Make things move>姐妹篇,强烈推 ...
随机推荐
- IE10、IE11 无法写入Cookie
IE10.IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题 你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的, ...
- .net 添加Cookie的4种方法
第一种添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Values["a&qu ...
- 创建对象的最好方式&最好的继承机制(代码实例)
/* 创建对象的最好方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ function People(sname){ this.nam ...
- Jersey框架一:Jersey RESTful WebService框架简介
Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...
- selenium+python自动化之操作浏览器
一.打开网站 1.第一步:从selenium里面导入webdriver模块 2.打开Firefox浏览器(Ie和Chrome对应下面的) 3.打开百度网址 二.设置休眠 1.由于打开百度网址后,页面加 ...
- SharePoint 2010 最佳实践学习总结------第1章 SharePoint Foundation开发基础
----前言 这段时间项目出在验收阶段,不是很忙,就潜心把SharePoint学一下,不求有多深刻,初衷只是先入门再说.后续会发布一系列的学习总结.主要学习的书籍为<SharePoint2010 ...
- MongoDB备份与导入
导出mongodb的数据 mongodump -d nodes -o url 导入mongodb数据mongorestore --db nodes url 删除mongodb的数据库db.dropDa ...
- 用Opera Mobile调试手机版网页【转】
注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...
- 设计师必看的10个HTML5动画工具
如果你想用令人难以置信的动画创建引人注目的网站的话,那么这里为设计师精心挑选了一些必备的HTML5动画工具.HTML5是设计师用来打造时尚网站的最流行的编程语言之一.在过去三年内,这种编程语言的使用人 ...
- POI按照源单元格设置目标单元格格式
原文:http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/ POI按照源单元格设置目标单元格格式 poi按照一个源单 ...