输入框和输出框。用户名、输入框是 Input Text,实例名称分别为 Name_txt 和 Input_txt。输出框是 Dynamic Text ,实例名称是 Msg_txt。
还有一个按钮组件。Click Handler 设定为 chat。
然后,开始编码。在第一帧上加入如下代码:
#include "NetDebug.as" #include "NetServices.as" stop(); //================================== nc = new NetConnection(); nc.connect("rtmp:/elearning"); //==================================== text_so = SharedObject.getRemote("sharedtext", nc.uri, false); text_so.connect(nc); text_so.onSync = function(list) { Msg_txt.text = Msg_txt.text + "/n" + text_so.data.msg; }; function chat() { if (Input_txt.text != "") { text_so.data.msg = Name_txt.text + " 说:" + Input_txt.text; Input_txt.text = ""; } }
我们来分析一下这些代码。
#include "NetDebug.as"
#include "NetServices.as"
这行两行代码是用来分析代码错误;引入连接服务的。
nc = new NetConnection();
nc.connect("rtmp:/elearning");
建立一个新的连接,并让他通过 rtmp 协议连接到 elearning 这个应用实例上。需要事先在 FlashCom 目录下的 applications 目录中建立一个名称为 elearning 的文件夹。
text_so = SharedObject.getRemote("sharedtext", nc.uri, false);
text_so.connect(nc);
这里我们创建了一个 SharedObject ,并连接到 nc 这个连接上。SharedObject 是 FlashCom 中特有的对象。它可以让所有客户端实时的得到同一的数据。
text_so.onSync = function(list) {
Msg_txt.text = Msg_txt.text + "/n" + text_so.data.msg;
};
这个函数的目的就是让所有使用 text_so 这个共享对象的客户端,在 text_so 改变的时候,接收数据。
function chat() {
if (Input_txt.text != "") {
text_so.data.msg = Name_txt.text + " 说:" + Input_txt.text;
Input_txt.text = "";
}
}
这是发送聊天者文字的函数,就是将输入的文字赋予 text_so.data.msg 中,然后清空输入文本框。
好了!现在你可以同时打开两个这样的影片,看看结果。做一个聊天室就是这么简单!
以上为对文章的引用,引用自http://www.flashempire.com/school/tutorview.php?id=141
好了,在使用:
#include "NetDebug.as"
#include "NetServices.as"
的时候,会出现找不到文件的现像。出现这种问题的原因为:
无论如何都出现"Error opening include file NetServices.as: File not found."和NetDebug.as not found的错误.查了半天google在在这里发现原因,原来这些例子都是面向Flash MX的,而#include在MX2004中是不被推荐的用法( #include "NetServices.as has been depracated in MX 2004. Try this, import mx.remoting.NetServices;), 虽然没仔细查到错误原因(估计是和路径有关系,因为Classes在$sys$:/Documents and Settings下同样有套备份,如果将这两个文件拷贝到发布目录就会继续报错说这个目录下无法找到NetServices.as中引用的一些文件.)所 以说解决方法是应该使用"import mx.remoting.*;"这样的格式.如此编译成功.