【问题标题】:Sharing data between ActionScript clients在 ActionScript 客户端之间共享数据
【发布时间】:2013-05-26 23:43:27
【问题描述】:

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

Google 告诉我使用 SharedObject。但我不知道怎么做。此外,我不知道它是否符合我的要求。

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

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

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

谢谢!

【问题讨论】:

    标签: actionscript-3 multiplayer shared-objects


    【解决方案1】:

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

    据我所知,SharedObject 用于在同一页面上的 2 个单独的闪存实例之间进行通信。 编辑:除非您正在运行可以利用远程通信的 Adob​​e Flash 媒体服务器。

    这是一个简单的例子,将数据发送到 php 并让它返回一些东西。您可以自定义服务器端逻辑以按照您的需要工作。

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

    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.
    
    ?>
    

    【讨论】:

    • 哦,好的。非常感谢。我已经这样做了,但我认为有更简单的方法。谢谢!
    • 在阅读了 SharedObject 类之后,它确实能够与客户端进行远程通信,但是您需要一个 Adob​​e Flash Media Server 来利用这些功能。
    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多