Actionscript 3 如何在AS3和Red5中使用远程SharedObject
我希望使用远程SharedObject,所以我创建了一个简单的脚本来测试这些技术。当我将以下代码作为SWF的两个实例运行时,两个实例都输出1,这是不正确的,因为第二个实例应该输出2Actionscript 3 如何在AS3和Red5中使用远程SharedObject,actionscript-3,flash,actionscript,red5,shared-objects,Actionscript 3,Flash,Actionscript,Red5,Shared Objects,我希望使用远程SharedObject,所以我创建了一个简单的脚本来测试这些技术。当我将以下代码作为SWF的两个实例运行时,两个实例都输出1,这是不正确的,因为第二个实例应该输出2 import flash.net.SharedObject; import flash.events.SyncEvent; var nc:NetConnection; var so:SharedObject; nc = new NetConnection(); nc.client = { onBWDone:
import flash.net.SharedObject;
import flash.events.SyncEvent;
var nc:NetConnection;
var so:SharedObject;
nc = new NetConnection();
nc.client = { onBWDone: function():void{} };
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect("rtmp://localhost:1935/live");
var t = new TextField();
addChild(t);
function onNetStatus(event:NetStatusEvent):void{
if(event.info.code == "NetConnection.Connect.Success"){
so = SharedObject.getRemote("shObj",nc.uri);
so.connect(nc);
if (!(so.data.total > 0 && so.data.total<1000)) {// undefined
so.data.total=1;
} else so.data.total=2;
t.text=so.data.total;
}
}
基本上,为了使用共享对象,我建议将作业分成三个独立的部分
function onNetStatus(event:NetStatusEvent):void
{
if(event.info.code == "NetConnection.Connect.Success")
{
so = SharedObject.getRemote("shObj",nc.uri);
so.addEventListener(SyncEvent.SYNC,syncHandler); //add event listener for Shared Object
so.connect(nc);
}
}
/* This function is called whenever there is change in Shared Object data */
function syncHandler(event:SyncEvent):void
{
if(so.data.total) //if total field exists in the Shared Object
trace(so.data.total);
}
在这里使用共享对象的方法。当您需要更改值时(可能在单击按钮时或发生特定事件时)调用此方法
我有一组使用SO的完整示例,这是我在过去几天中刚刚写的:
/* This function is called whenever there is change in Shared Object data */
function syncHandler(event:SyncEvent):void
{
if(so.data.total) //if total field exists in the Shared Object
trace(so.data.total);
}
/* This function writes values to the Shared Object */
function changeValue(newValue:String)
{
so.setProperty("total",newValue);
}