在ActionScript客户端之间共享数据

时间:2013-05-25 22:41:22

标签: actionscript-3 multiplayer shared-objects

我正在使用ActionScript 3编写一个在线多人游戏基于文本的游戏,并试图找到一种创建单个对象的方法,并允许所有玩家即时修改它(我的意思是,当它被修改时,其他客户将看到它的新价值。)

谷歌告诉我使用SharedObject。但我不知道怎么做。而且,我不知道它是否符合我的要求。

所以把它想象成一个聊天室,一个对象(ChatRoom,有一个成员Text:String

我该如何实现这样的事情? :

function UserPressedEnter(event:KeyboardEvent,username:String,message:String)
{
 ChatRoom.Text += "\n"+username+" : "+message;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我正确理解这一点,您可能必须使用一些服务器端代码并​​从客户端来回发送消息。

据我所知,SharedObject用于在同一页面上的两个独立的flash实例之间进行通信。 编辑:除非您正在运行可以利用远程通信的Adobe Flash Media Sever。

这是一个将数据发送到php并让它返回内容的简单示例。您可以自定义服务器端逻辑,以满足您的需求。

如果这需要超级快速,您可能会考虑使用Redis而不是PHP来设置节点服务器。

<强> AS

function UserPressedEnter(event:KeyboardEvent, username:String, message:String):void {
  var action:String = "http://www.mywebserver.com/save_messages.php";
  var loader:URLLoader = new URLLoader();
  loader.dataFormat = URLLoaderDataFormat.VARIABLES;

  var request:URLRequest = new URLRequest(action);
  request.method = URLRequestMethod.POST;      
  request.data = URLVariables({username:username, message:message});

  loader.addEventListener(Event.COMPLETE, completeHandler);
  loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpResponseHandler);
  loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  loader.addEventListener(Event.OPEN, openHandler);
  loader.load(request);
}

function completeHandler(event:Event):void {
  trace("");
  trace("******************************************** DATA RETRIEVED VIA OUTPUT RESPONSE *************************************************");
  trace(event.target.data);
  trace("*********************************************************************************************************************************");
  trace("");

  trace("Data returned by server: " + event.target.data);

  ChatRoom.Text += "\n" + event.target.data.username + " : " + event.target.data.message;

  var loader:URLLoader = event.target as URLLoader;
  loader.removeEventListener(Event.COMPLETE, completeHandler);
  loader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpResponseHandler);
  loader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
  loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  loader.removeEventListener(Event.OPEN, openHandler);
}

PHP (“action”var以上的“save_messages.php”)

<?php

  //Flash will send out name/value pairs in a POST/GET (post recommended)
  if (sizeof($_POST) > 0 || sizeof($_GET) > 0) {
    //If the POST/GET header has elements set success to true and print out the elements
    echo("success=true&");

    //The following simply parrots the values back to flash. Instead of printing out the array you could set up a DB and have them inserted or set up a web proxy to send the data elsewhere. 
    foreach ($_POST as $pKey => $pValue) {
      echo($pKey."=".$pValue."&");
    }

    foreach ($_GET as $gKey => $gValue) {
      echo($gKey."=".$gValue."&");
    }
  }
  else {
    //If the POST/GET header does not contain any information set success to false.
    echo("success=false&");
  }

  //Prints "complete" to let flash know the page has finished processing.
  echo("complete=true");

  //Flash will then read the echoed text which will be something like:
  //success=true&NAME=VALUE&NAME2=VALUE2&complete=true;

  //The "success" and "complete" vars are helpful in testing, please make sure they are accounted for.

?>